<HTML ><HEAD ><TITLE >Passing by Reference</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="References Explained" HREF="language.references.html"><LINK REL="PREVIOUS" TITLE="What References Are Not" HREF="language.references.arent.html"><LINK REL="NEXT" TITLE="Returning References" HREF="language.references.return.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.references.arent.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 15. References Explained</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.references.return.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.references.pass" ></A >Passing by Reference</H1 ><P > You can pass variable to function by reference, so that function could modify its arguments. The syntax is as follows: <DIV CLASS="informalexample" ><A NAME="AEN5656" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function foo (&$var) { $var++; } $a=5; foo ($a); // $a is 6 here</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > Note that there's no reference sign on function call - only on function definition. Function definition alone is enough to correctly pass the argument by reference. </P ><P > Following things can be passed by reference: <P ></P ><UL ><LI ><P > Variable, i.e. <TT CLASS="literal" >foo($a)</TT > </P ></LI ><LI ><P > New statement, i.e. <TT CLASS="literal" >foo(new foobar())</TT > </P ></LI ><LI ><P > Reference, returned from a function, i.e.: <DIV CLASS="informalexample" ><A NAME="AEN5668" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function &bar() { $a = 5; return $a; } foo(bar());</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > See also explanations about <A HREF="language.references.return.html" >returning by reference</A >. </P ></LI ></UL > </P ><P > Any other expression should not be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid: <DIV CLASS="informalexample" ><A NAME="AEN5672" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function bar() // Note the missing & { $a = 5; return $a; } foo(bar()); foo($a = 5) // Expression, not variable foo(5) // Constant, not variable</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > These requirements are for PHP 4.0.4 and later. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.references.arent.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.references.return.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >What References Are Not</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.references.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Returning References</TD ></TR ></TABLE ></DIV ></BODY ></HTML >