<!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>Assignment Operators</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.operators.arithmetic.html">Arithmetic Operators</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.operators.bitwise.html">Bitwise Operators</a></div> <div class="up"><a href="language.operators.html">Operators</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.operators.assignment" class="sect1"> <h2 class="title">Assignment Operators</h2> <p class="simpara"> The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to"). </p> <p class="para"> The value of an assignment expression is the value assigned. That is, the value of "<em>$a = 3</em>" is 3. This allows you to do some tricky things: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$a </span><span style="color: #007700">= (</span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">) + </span><span style="color: #0000BB">5</span><span style="color: #007700">; </span><span style="color: #FF8000">// $a is equal to 9 now, and $b has been set to 4.<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> For <span class="type"><span class="type arrays">arrays</span></span>, assigning a value to a named key is performed using the "=>" operator. The <a href="language.operators.precedence.html" class="link">precedence</a> of this operator is the same as other assignment operators. </p> <p class="para"> In addition to the basic assignment operator, there are "combined operators" for all of the <a href="language.operators.html" class="link">binary arithmetic</a>, array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">3</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">; </span><span style="color: #FF8000">// sets $a to 8, as if we had said: $a = $a + 5;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #DD0000">"Hello "</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">.= </span><span style="color: #DD0000">"There!"</span><span style="color: #007700">; </span><span style="color: #FF8000">// sets $b to "Hello There!", just like $b = $b . "There!";<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop. </p> <p class="para"> An exception to the usual assignment by value behaviour within PHP occurs with <span class="type"><a href="language.types.object.html" class="type object">object</a></span>s, which are assigned by reference in PHP 5. Objects may be explicitly copied via the <a href="language.oop5.cloning.html" class="link">clone</a> keyword. </p> <div class="sect2" id="language.operators.assignment.reference"> <h3 class="title">Assignment by Reference</h3> <p class="para"> Assignment by reference is also supported, using the "<span class="computeroutput">$var = &$othervar;</span>" syntax. Assignment by reference means that both variables end up pointing at the same data, and nothing is copied anywhere. </p> <p class="para"> <div class="example" id="example-117"> <p><strong>Example #1 Assigning by reference</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= &</span><span style="color: #0000BB">$a</span><span style="color: #007700">; </span><span style="color: #FF8000">// $b is a reference to $a<br /><br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints 3<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints 3<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">; </span><span style="color: #FF8000">// change $a<br /><br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints 4<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints 4 as well, since $b is a reference to $a, which has<br /> // been changed<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> As of PHP 5, the <a href="language.oop5.basic.html#language.oop5.basic.new" class="link">new</a> operator returns a reference automatically, so assigning the result of <a href="language.oop5.basic.html#language.oop5.basic.new" class="link">new</a> by reference results in an <strong><code>E_DEPRECATED</code></strong> message in PHP 5.3 and later, and an <strong><code>E_STRICT</code></strong> message in earlier versions. </p> <p class="para"> For example, this code will result in a warning: <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">class </span><span style="color: #0000BB">C </span><span style="color: #007700">{}<br /><br /></span><span style="color: #FF8000">/* The following line generates the following error message:<br /> * Deprecated: Assigning the return value of new by reference is deprecated in...<br /> */<br /></span><span style="color: #0000BB">$o </span><span style="color: #007700">= &new </span><span style="color: #0000BB">C</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> More information on references and their potential uses can be found in the <a href="language.references.html" class="link">References Explained</a> section of the manual. </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.operators.arithmetic.html">Arithmetic Operators</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.operators.bitwise.html">Bitwise Operators</a></div> <div class="up"><a href="language.operators.html">Operators</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>