Source of: /manual/en/language.references.pass.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = 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 => 'en',
),
'this' =>
array (
0 => 'language.references.pass.php',
1 => 'Passing by Reference',
),
'up' =>
array (
0 => 'language.references.php',
1 => 'References Explained',
),
'prev' =>
array (
0 => 'language.references.arent.php',
1 => 'What References Are Not',
),
'next' =>
array (
0 => 'language.references.return.php',
1 => 'Returning References',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<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:
</p><div class="informalexample">
<div class="example-contents programlisting">
<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><p>
</p><blockquote><p><b class="note">Note</b>:
<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 <i>foo(&$a);</i>.
</span>
</p></blockquote><p>
</p>
<p class="para">
The following things can be passed by reference:
</p><ul class="itemizedlist">
<li class="listitem">
<span class="simpara">
Variables, i.e. <i>foo($a)</i>
</span>
</li>
<li class="listitem">
<span class="simpara">
New statements, i.e. <i>foo(new foobar())</i>
</span>
</li>
<li class="listitem">
<p class="para">
References returned from functions, i.e.:
</p><div class="informalexample">
<div class="example-contents programlisting">
<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">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><p>
See more about <a href="language.references.return.php" class="link">returning by reference</a>.
</p>
</li>
</ul><p>
</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:
</p><div class="informalexample">
<div class="example-contents programlisting">
<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">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><p>
These requirements are for PHP 4.0.4 and later.
</p>
</div><?php manual_footer(); ?>