Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason different articles seem to be saying different things is that they are talking about different kinds of pass-by-reference.</p> <p>The main thing that determines if a parameter should be passed by reference is the function signature itself, and the fundamentals of this have not changed since PHP 4. Consider this example:</p> <pre><code>function foo( $by_value, &amp;$by_reference ) { /* ... */ } $a = 1; $b = 2; foo( $a, $b ); </code></pre> <p>Here, the outer variable <code>$a</code> is being passed to the function by value, as though it is being assigned as <code>$by_value = $a;</code> - changes to <code>$by_value</code> cannot affect <code>$a</code>. The variable <code>$b</code> however is being passed <em>by reference</em>; just like an assignment of the form <code>$by_reference =&amp; $b;</code> this means that there is one variable referenced by two names, and any assignment to one will act as an assignment to both.</p> <p>If you pass an "ordinary" value (a string, number, or array) by value, its value is just copied to the new variable. <strong>As of PHP 5</strong>: If you pass an object by value, however, something slightly different happens - the "value" copied is just a pointer to the same object. This means that if <code>$a</code> were an object, you could call <code>$by_value-&gt;some_property = 42;</code> and <code>$a-&gt;some_property</code> would also be <code>42</code>. However, if you assigned some new value to <code>$by_value</code>, it would still not affect <code>$a</code>.</p> <p><strong>Until PHP 5.4</strong>, there was an <em>extra</em> way to pass a parameter by reference, which was to "force" the reference behaviour <em>at call-time</em>. This meant that you could write <code>foo(&amp;$a, &amp;$b);</code> and "capture" changes made to <code>$by_value</code> inside the <code>foo()</code> function. Relying on this was generally a bad idea, and so it was removed. (It landed in 5.4 because it was intended for removal in PHP 6, but that project was put on indefinite hold, with the smaller changes landing in 5.3 and 5.4).</p> <p>Finally, functions can <em>return</em> a variable by reference (as <a href="http://php.net/references.return" rel="noreferrer">discussed in the manual here</a>). This is a little fiddly, as it actually requires you to put <code>&amp;</code> in <em>two</em> places: at the beginning of the function declaration, to say that <code>return</code> should mean "return this variable reference" not "return this value"; and in the code calling it, to assign a variable to that reference, rather than just copying its value. Here's a silly example which combines a reference parameter with a reference return (the two do not have to go together, it's just an example):</p> <pre><code>function &amp;bar(&amp;$some_param) { return $some_param; } $a = 1; $b =&amp; bar($a); // $b and $a now point at the same variable, not just the same value // it was passed into and out of a function, and assigned to a new variable, // but all those operations were by reference </code></pre> <p>Note that many people mistakenly believe that passing a variable by reference will give them a performance benefit, and this was often their only reason for using call-time pass-by-reference. This is in fact usually wrong, as the Zend Engine which powers PHP uses a technique called "copy on write" to leave multiple variables which happen to have the same value pointing at the same piece of memory, even if they are not bound up as references. In fact, reference assignment generally <em>defeats</em> this optimisation, due to the way the engine tracks which variables are in the copy-on-write state.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload