Source of: /manual/en/language.operators.assignment.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.operators.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'language.operators.assignment.php',
1 => 'Assignment Operators',
),
'up' =>
array (
0 => 'language.operators.php',
1 => 'Operators',
),
'prev' =>
array (
0 => 'language.operators.arithmetic.php',
1 => 'Arithmetic Operators',
),
'next' =>
array (
0 => 'language.operators.bitwise.php',
1 => 'Bitwise Operators',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<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
rights (that is, "gets set to").
</p>
<p class="para">
The value of an assignment expression is the value assigned. That
is, the value of "<i>$a = 3</i>" is 3. This allows you to do some tricky
things:
</p><div class="informalexample">
<div class="example-contents programlisting">
<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>
<p class="para">
In addition to the basic assignment operator, there are "combined
operators" for all of the <a href="language.operators.php" 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:
</p><div class="informalexample">
<div class="example-contents programlisting">
<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>
<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. 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.
To learn more about references, please read <a href="language.references.php" class="link">References explained</a>. As of
PHP 5, objects are assigned by reference unless explicitly told
otherwise with the new <a href="language.oop5.cloning.php" class="link">clone</a>
keyword.
</p>
</div><?php manual_footer(); ?>