downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

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 &quot;=&quot;. Your first inclination might
    be to think of this as &quot;equal to&quot;. Don&#039;t. It really means that the
    left operand gets set to the value of the expression on the
    rights (that is, &quot;gets set to&quot;).
   </p>
   <p class="para">
    The value of an assignment expression is the value assigned. That
    is, the value of &quot;<i>$a = 3</i>&quot; 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">&lt;?php<br /><br />$a&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">)&nbsp;+&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;is&nbsp;equal&nbsp;to&nbsp;9&nbsp;now,&nbsp;and&nbsp;$b&nbsp;has&nbsp;been&nbsp;set&nbsp;to&nbsp;4.<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div><p>
   </p>
   <p class="para">
    In addition to the basic assignment operator, there are &quot;combined
    operators&quot; 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">&lt;?php<br /><br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;sets&nbsp;$a&nbsp;to&nbsp;8,&nbsp;as&nbsp;if&nbsp;we&nbsp;had&nbsp;said:&nbsp;$a&nbsp;=&nbsp;$a&nbsp;+&nbsp;5;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"There!"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;sets&nbsp;$b&nbsp;to&nbsp;"Hello&nbsp;There!",&nbsp;just&nbsp;like&nbsp;$b&nbsp;=&nbsp;$b&nbsp;.&nbsp;"There!";<br /><br /></span><span style="color: #0000BB">?&gt;</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 =
    &amp;$othervar;</span> syntax.
    &#039;Assignment by reference&#039; 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(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites