<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.references.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'language.references.whatdo.php',
    1 => 'What References Do',
    2 => 'What References Do',
  ),
  'up' => 
  array (
    0 => 'language.references.php',
    1 => 'References Explained',
  ),
  'prev' => 
  array (
    0 => 'language.references.whatare.php',
    1 => 'What References Are',
  ),
  'next' => 
  array (
    0 => 'language.references.arent.php',
    1 => 'What References Are Not',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/references.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.references.whatdo" class="sect1">
   <h2 class="title">What References Do</h2>
   <p class="para">
    There are three basic operations performed using references:
    <a href="language.references.whatdo.php#language.references.whatdo.assign" class="link">assigning by
    reference</a>, <a href="language.references.whatdo.php#language.references.whatdo.pass" class="link">passing
    by reference</a>,
    and <a href="language.references.whatdo.php#language.references.whatdo.return" class="link">returning by
    reference</a>. This section will give an introduction to these
    operations, with links for further reading.
   </p>
   <div class="sect2" id="language.references.whatdo.assign">
    <h3 class="title">Assign By Reference</h3>
    <p class="para">
     In the first of these, PHP references allow you to make two
     variables refer to the same content. Meaning, when you do:
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$a </span><span style="color: #007700">=&amp; </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
     it means that <var class="varname">$a</var> and <var class="varname">$b</var>
     point to the same content.
     <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
      <p class="para">
       <var class="varname">$a</var> and <var class="varname">$b</var> are completely
       equal here. <var class="varname">$a</var> is not pointing to
       <var class="varname">$b</var> or vice versa.
       <var class="varname">$a</var> and <var class="varname">$b</var> are pointing to the
       same place.
      </p>
     </p></blockquote>
    </p>
    <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
     <p class="para">
      If you assign, pass, or return an undefined variable by reference, 
      it will get created.
      <div class="example" id="example-1">
       <p><strong>Приклад #1 Using references with undefined variables</strong></p>
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$var</span><span style="color: #007700">) {}<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">); </span><span style="color: #FF8000">// $a is "created" and assigned to null<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= array();<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">[</span><span style="color: #DD0000">'b'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'b'</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">)); </span><span style="color: #FF8000">// bool(true)<br /><br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= new </span><span style="color: #0000BB">stdClass</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">d</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">property_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">, </span><span style="color: #DD0000">'d'</span><span style="color: #007700">)); </span><span style="color: #FF8000">// bool(true)<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
       </div>

      </div>
     </p>
    </p></blockquote>
    <p class="para">
     The same syntax can be used with functions that return
     references:
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$foo </span><span style="color: #007700">=&amp; </span><span style="color: #0000BB">find_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
    <p class="para">
     Using the same syntax with a function that does <em>not</em>
     return by reference will give an error, as will using it with the result
     of the <a href="language.oop5.basic.php#language.oop5.basic.new" class="link">new</a> operator.
     Although objects are passed around as pointers, these are not the same as references,
     as explained under <a href="language.oop5.references.php" class="link">Objects and references</a>.
    </p>
    <div class="warning"><strong class="warning">Увага</strong>
     <p class="para">
      If you assign a reference to a variable declared <code class="literal">global</code>
      inside a function, the reference will be visible only inside the function.
      You can avoid this by using the <var class="varname"><a href="reserved.variables.globals.php" class="classname">$GLOBALS</a></var> array.
      <div class="example" id="example-2">
       <p><strong>Приклад #2 Referencing global variables inside functions</strong></p>
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$var1 </span><span style="color: #007700">= </span><span style="color: #DD0000">"Example variable"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$var2 </span><span style="color: #007700">= </span><span style="color: #DD0000">""</span><span style="color: #007700">;<br /><br />function </span><span style="color: #0000BB">global_references</span><span style="color: #007700">(</span><span style="color: #0000BB">$use_globals</span><span style="color: #007700">)<br />{<br />    global </span><span style="color: #0000BB">$var1</span><span style="color: #007700">, </span><span style="color: #0000BB">$var2</span><span style="color: #007700">;<br /><br />    if (!</span><span style="color: #0000BB">$use_globals</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$var2 </span><span style="color: #007700">=&amp; </span><span style="color: #0000BB">$var1</span><span style="color: #007700">; </span><span style="color: #FF8000">// visible only inside the function<br />    </span><span style="color: #007700">} else {<br />        </span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">"var2"</span><span style="color: #007700">] =&amp; </span><span style="color: #0000BB">$var1</span><span style="color: #007700">; </span><span style="color: #FF8000">// visible also in global context<br />    </span><span style="color: #007700">}<br />}<br /><br /></span><span style="color: #0000BB">global_references</span><span style="color: #007700">(</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"var2 is set to '</span><span style="color: #0000BB">$var2</span><span style="color: #DD0000">'\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// var2 is set to ''<br /><br /></span><span style="color: #0000BB">global_references</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"var2 is set to '</span><span style="color: #0000BB">$var2</span><span style="color: #DD0000">'\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// var2 is set to 'Example variable'<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
       </div>

      </div>
      Think about <code class="literal">global $var;</code> as a shortcut to <code class="literal">$var
        =&amp; $GLOBALS[&#039;var&#039;];</code>. Thus assigning another reference
      to <code class="literal">$var</code> only changes the local variable&#039;s reference.
     </p>
    </div>
    <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
     <p class="para">
      If you assign a value to a variable with references in a 
      <a href="control-structures.foreach.php" class="link">foreach</a> statement, the references are modified too.
      <div class="example" id="example-3">
       <p><strong>Приклад #3 References and foreach statement</strong></p>
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$ref </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$row </span><span style="color: #007700">=&amp; </span><span style="color: #0000BB">$ref</span><span style="color: #007700">;<br /><br />foreach (array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">) as </span><span style="color: #0000BB">$row</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">// Do something<br /></span><span style="color: #007700">}<br /><br />echo </span><span style="color: #0000BB">$ref</span><span style="color: #007700">; </span><span style="color: #FF8000">// 3 - last element of the iterated array<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
       </div>

      </div>
     </p>
    </p></blockquote>
    <p class="para">
     While not being strictly an assignment by reference, expressions created
     with the language construct
     <a href="function.array.php" class="link"><code class="literal">array()</code></a> can also
     behave as such by prefixing <code class="literal">&amp;</code> to the array element
     to add. Example:
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$arr </span><span style="color: #007700">= array(&amp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">, &amp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">], &amp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]++;<br /></span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]++;<br /></span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]++;<br /></span><span style="color: #FF8000">/* $a == 2, $b == array(3, 4); */<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
    <p class="para">
     Note, however, that references inside arrays are potentially dangerous.
     Doing a normal (not by reference) assignment with a reference on the
     right side does not turn the left side into a reference, but references
     inside arrays are preserved in these normal assignments. This also applies
     to function calls where the array is passed by value. Example:
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/* Assignment of scalar variables */<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">=&amp; </span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">7</span><span style="color: #007700">; </span><span style="color: #FF8000">// $c is not a reference; no change to $a or $b<br /><br />/* Assignment of array variables */<br /></span><span style="color: #0000BB">$arr </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">=&amp; </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]; </span><span style="color: #FF8000">// $a and $arr[0] are in the same reference set<br /></span><span style="color: #0000BB">$arr2 </span><span style="color: #007700">= </span><span style="color: #0000BB">$arr</span><span style="color: #007700">; </span><span style="color: #FF8000">// Not an assignment-by-reference!<br /></span><span style="color: #0000BB">$arr2</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]++;<br /></span><span style="color: #FF8000">/* $a == 2, $arr == array(2) */<br />/* The contents of $arr are changed even though it's not a reference! */<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
     In other words, the reference behavior of arrays is defined in an
     element-by-element basis; the reference behavior of individual elements
     is dissociated from the reference status of the array container.
    </p>
   </div>
   <div class="sect2" id="language.references.whatdo.pass">
    <h3 class="title">Pass By Reference</h3>
    <p class="para">
     The second thing references do is to pass variables by
     reference. This is done by making a local variable in a function
     and a variable in the calling scope referencing the same
     content. Example:
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$var</span><span style="color: #007700">)<br />{<br />    </span><span style="color: #0000BB">$var</span><span style="color: #007700">++;<br />}<br /><br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">=</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
     will make <var class="varname">$a</var> to be 6. This happens because in
     the function <var class="varname">foo</var> the variable
     <var class="varname">$var</var> refers to the same content as
     <var class="varname">$a</var>. For more information on this, read
     the <a href="language.references.pass.php" class="link">passing by
       reference</a> section.
    </p>
   </div>
   <div class="sect2" id="language.references.whatdo.return">
    <h3 class="title">Return By Reference</h3>
    <p class="para">
     The third thing references can do is <a href="language.references.return.php" class="link">return by reference</a>.
    </p>
   </div>
  </div><?php manual_footer($setup); ?>