<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Passing by Reference</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.references.arent.html">What References Are Not</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.references.return.html">Returning References</a></div> <div class="up"><a href="language.references.html">References Explained</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.references.pass" class="sect1"> <h2 class="title">Passing by Reference</h2> <p class="para"> You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(&</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 /></span><span style="color: #FF8000">// $a is 6 here<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in <em>foo(&$a);</em>. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error. </span> </p></blockquote> </p> <p class="para"> The following things can be passed by reference: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> Variables, i.e. <em>foo($a)</em> </span> </li> <li class="listitem"> <span class="simpara"> New statements, i.e. <em>foo(new foobar())</em> </span> </li> <li class="listitem"> <p class="para"> References returned from functions, i.e.: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(&</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 />function &</span><span style="color: #0000BB">bar</span><span style="color: #007700">()<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 /> return </span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">bar</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> See more about <a href="language.references.return.html" class="link">returning by reference</a>. </p> </li> </ul> </p> <p class="para"> No other expressions should be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(&</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 />function </span><span style="color: #0000BB">bar</span><span style="color: #007700">() </span><span style="color: #FF8000">// Note the missing &<br /></span><span style="color: #007700">{<br /> </span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /> return </span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">bar</span><span style="color: #007700">()); </span><span style="color: #FF8000">// Produces fatal error since PHP 5.0.5<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: #0000BB">5</span><span style="color: #007700">); </span><span style="color: #FF8000">// Expression, not variable<br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">); </span><span style="color: #FF8000">// Produces fatal error<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> These requirements are for PHP 4.0.4 and later. </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.references.arent.html">What References Are Not</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.references.return.html">Returning References</a></div> <div class="up"><a href="language.references.html">References Explained</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>