Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, <a href="http://php.net/manual/en/language.references.arent.php" rel="noreferrer">references are not pointers</a>.</p> <p>I tried the code given by @John in his answer, but I got strange results. It turns out <code>microtime()</code> returns a string. Arithmetic is unreliable and I even got negative results on some runs. One should use <code>microtime(true)</code> to get the value as a float.</p> <p>I added another test of no function call, just incrementing the variable:</p> <pre><code>&lt;?php $param = 1; $start = microtime(true); for($i = 1; $i &lt;= 1000000; $i++) { $param++; } $end = microtime(true); echo "increment: " . ($end - $start) . "\n"; </code></pre> <p>The results on my machine, Macbook 2.4GHz running PHP 5.3.2.</p> <ul> <li>function call with pass by reference: <strong>2.14 sec.</strong></li> <li>function call with pass by value: <strong>2.26 sec.</strong></li> <li>no function call, just bare increment: <strong>0.42 sec.</strong></li> </ul> <p>So there seems to be a 5.3% performance advantage to passing by reference, but there is a 81% performance advantage to avoiding the function call completely.</p> <p>I guess the example of incrementing an integer is arbitrary, and the OP is really asking about the general advantage of passing by reference. But I'm just offering this example to demonstrate that <strong>the function call alone incurs far more overhead than the method of passing parameters.</strong></p> <p>So if you're trying to decide how to micro-optimize parameter passing, you're being penny wise and pound foolish.</p> <p>There are also other reasons why you should avoid references. Though they can simplify several algorithms, especially when you are manipulating two or more data structures that must have the same underlying data:</p> <ul> <li>They make functions have side-effects. You should, in general, avoid functions with side-effects, as they make the program more unpredictable (as in "OK, how this did this value get here? did any of the functions modify its parameters?")</li> <li>They cause bugs. If you make a variable a reference, you must remember to unset it before assigning it a value, unless you want to change the underlying value of the reference set. This happens frequently after you run a foreach loop by reference and then re-use the loop variable.</li> </ul>
    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.
 

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