<HTML ><HEAD ><TITLE >Variables</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.44"><LINK REL="HOME" TITLE="PHP Manual" HREF="manual.html"><LINK REL="UP" TITLE="Language Reference" HREF="langref.html"><LINK REL="PREVIOUS" TITLE="Type juggling" HREF="language.types.type-juggling.html"><LINK REL="NEXT" TITLE="Predefined variables" HREF="language.variables.predefined.html"></HEAD ><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><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.types.type-juggling.html" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.variables.predefined.html" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.variables" >Chapter 7. Variables</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="language.variables.html#language.variables.basics" >Basics</A ></DT ><DT ><A HREF="language.variables.predefined.html" >Predefined variables</A ></DT ><DT ><A HREF="language.variables.scope.html" >Variable scope</A ></DT ><DT ><A HREF="language.variables.variable.html" >Variable variables</A ></DT ><DT ><A HREF="language.variables.external.html" >Variables from outside PHP</A ></DT ></DL ></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.variables.basics" >Basics</A ></H1 ><P > Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. <DIV CLASS="informalexample" ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 $var = "Bob"; 3 $Var = "Joe"; 4 echo "$var, $Var"; // outputs "Bob, Joe" 5 </PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > In PHP3, variables are always assigned by value. That is to say, when you assign an expression to a variable, the entire value of the original expression is copied into the destination variable. This means, for instance, that after assigning one variable's value to another, changing one of those variables will have no effect on the other. For more information on this kind of assignment, see <A HREF="language.expressions.html" >Expressions</A >. </P ><P > PHP4 offers another way to assign values to variables: <I CLASS="emphasis" >assign by reference</I >. This means that the new variable simply references (in other words, "becomes an alias for" or "points to") the original variable. Changes to the new variable affect the original, and vice versa. This also means that no copying is performed; thus, the assignment happens more quickly. However, any speedup will likely be noticed only in tight loops or when assigning large arrays or objects. </P ><P > To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable). For instance, the following code snippet outputs 'My name is Bob' twice: <DIV CLASS="informalexample" ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 <?php 3 $foo = 'Bob'; // Assign the value 'Bob' to $foo 4 $bar = &$foo; // Reference $foo via $bar. 5 $bar = "My name is $bar"; // Alter $bar... 6 echo $foo; // $foo is altered too. 7 echo $bar; 8 ?> 9 </PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > One important thing to note is that only named variables may be assigned by reference. <DIV CLASS="informalexample" ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 <?php 3 $foo = 25; 4 $bar = &$foo; // This is a valid assignment. 5 $bar = &(24 * 7); // Invalid; references an unnamed expression. 6 7 function test() { 8 return 25; 9 } 10 11 $bar = &test(); // Invalid. 12 ?> 13 </PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.types.type-juggling.html" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="manual.html" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.variables.predefined.html" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Type juggling</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="langref.html" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Predefined variables</TD ></TR ></TABLE ></DIV ></BODY ></HTML >