<HTML ><HEAD ><TITLE >What References Do</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="References Explained" HREF="language.references.html"><LINK REL="NEXT" TITLE="What References Are Not" HREF="language.references.arent.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.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.arent.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.references.whatdo" ></A >What References Do</H1 ><P > PHP references allow you to make two variables to refer to the same content. Meaning, when you do: <DIV CLASS="informalexample" ><A NAME="AEN5601" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$a =& $b</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > it means that <TT CLASS="varname" >$a</TT > and <TT CLASS="varname" >$b</TT > point to the same variable. <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > <TT CLASS="varname" >$a</TT > and <TT CLASS="varname" >$b</TT > are completely equal here, that's not <TT CLASS="varname" >$a</TT > is pointing to <TT CLASS="varname" >$b</TT > or vice versa, that's <TT CLASS="varname" >$a</TT > and <TT CLASS="varname" >$b</TT > pointing to the same place. </P ></BLOCKQUOTE ></DIV > </P ><P > The same syntax can be used with functions, that return references, and with <TT CLASS="literal" >new</TT > operator (in PHP 4.0.4 and later): <DIV CLASS="informalexample" ><A NAME="AEN5615" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$bar =& new fooclass(); $foo =& find_var ($bar);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Not using the <TT CLASS="literal" >&</TT > operator causes a copy of the object to be made. If you use <TT CLASS="literal" >$this</TT > in the class it will operate on the current instance of the class. The assignment without <TT CLASS="literal" >&</TT > will copy the instance (i.e. the object) and <TT CLASS="literal" >$this</TT > will operate on the copy, which is not always what is desired. Usually you want to have a single instance to work with, due to performance and memory consumption issues. </P ><P > While you can use the <TT CLASS="literal" >@</TT > operator to <SPAN CLASS="emphasis" ><I CLASS="emphasis" >mute</I ></SPAN > any errors in the constructor when using it as <TT CLASS="literal" >@new</TT >, this does not work when using the <TT CLASS="literal" >&new</TT > statement. This is a limitation of the Zend Engine and will therefore result in a parser error. </P ></BLOCKQUOTE ></DIV ><P > The second thing references do is to pass variables by-reference. This is done by making a local variable in a function and a variable in the calling scope reference to the same content. Example: <DIV CLASS="informalexample" ><A NAME="AEN5629" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >function foo (&$var) { $var++; } $a=5; foo ($a);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > will make <TT CLASS="varname" >$a</TT > to be 6. This happens because in the function <TT CLASS="varname" >foo</TT > the variable <TT CLASS="varname" >$var</TT > refers to the same content as <TT CLASS="varname" >$a</TT >. See also more detailed explanations about <A HREF="language.references.pass.html" >passing by reference</A >. </P ><P > The third thing reference can do is <A HREF="language.references.return.html" >return by reference</A >. </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.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.arent.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >References Explained</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" >What References Are Not</TD ></TR ></TABLE ></DIV ></BODY ></HTML >