Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8938

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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 &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
    right (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;<em>$a = 3</em>&quot; 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">&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 class="para">
    For <span class="type"><span class="type arrays">arrays</span></span>, assigning a value to a named key is performed using
    the &quot;=&gt;&quot; 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 &quot;combined
    operators&quot; 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">&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 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
     &quot;<span class="computeroutput">$var = &amp;$othervar;</span>&quot; 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">&lt;?php<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">$b&nbsp;</span><span style="color: #007700">=&nbsp;&amp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$b&nbsp;is&nbsp;a&nbsp;reference&nbsp;to&nbsp;$a<br /><br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;prints&nbsp;3<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;prints&nbsp;3<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;change&nbsp;$a<br /><br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;prints&nbsp;4<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;prints&nbsp;4&nbsp;as&nbsp;well,&nbsp;since&nbsp;$b&nbsp;is&nbsp;a&nbsp;reference&nbsp;to&nbsp;$a,&nbsp;which&nbsp;has<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;been&nbsp;changed<br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #007700">{}<br /><br /></span><span style="color: #FF8000">/*&nbsp;The&nbsp;following&nbsp;line&nbsp;generates&nbsp;the&nbsp;following&nbsp;error&nbsp;message:<br />&nbsp;*&nbsp;Deprecated:&nbsp;Assigning&nbsp;the&nbsp;return&nbsp;value&nbsp;of&nbsp;new&nbsp;by&nbsp;reference&nbsp;is&nbsp;deprecated&nbsp;in...<br />&nbsp;*/<br /></span><span style="color: #0000BB">$o&nbsp;</span><span style="color: #007700">=&nbsp;&amp;new&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</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>