<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'language.operators.assignment.php',
    1 => 'Assignment',
    2 => 'Assignment Operators',
  ),
  'up' => 
  array (
    0 => 'language.operators.php',
    1 => 'Operators',
  ),
  'prev' => 
  array (
    0 => 'language.operators.increment.php',
    1 => 'Increment and Decrement',
  ),
  'next' => 
  array (
    0 => 'language.operators.bitwise.php',
    1 => 'Bitwise',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/operators/assignment.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<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;<code class="literal">$a = 3</code>&quot; is 3. This allows you to do some tricky
  things:
  <div class="example" id="example-1">
   <p><strong>Приклад #1 Nested Assignments</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<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 /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </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:
  <div class="example" id="example-2">
   <p><strong>Приклад #2 Combined Assignments</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">$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">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">);<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.php" class="type object">object</a></span>s, which are assigned by reference.
  Objects may be explicitly copied via the <a href="language.oop5.cloning.php" 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-3">
    <p><strong>Приклад #3 Assigning by reference</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">= &amp;</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">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   The <a href="language.oop5.basic.php#language.oop5.basic.new" class="link">new</a>
   operator returns a reference automatically, as such assigning the result of
   <a href="language.oop5.basic.php#language.oop5.basic.new" class="link">new</a> by reference is an error.
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>Приклад #4 new Operator By-Reference</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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: #0000BB">$o </span><span style="color: #007700">= &amp;new </span><span style="color: #0000BB">C</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Parse error: syntax error, unexpected token &quot;;&quot;, expecting &quot;(&quot;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   More information on references and their potential uses can be found in
   the <a href="language.references.php" class="link">References Explained</a>
   section of the manual.
  </p>
 </div>

 <div class="sect2" id="language.operators.assignment.arithmetic">
  <h3 class="title">Arithmetic Assignment Operators</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Example</th>
      <th>Equivalent</th>
      <th>Operation</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>$a += $b</td>
      <td>$a = $a + $b</td>
      <td>Addition</td>
     </tr>

     <tr>
      <td>$a -= $b</td>
      <td>$a = $a - $b</td>
      <td>Subtraction</td>
     </tr>

     <tr>
      <td>$a *= $b</td>
      <td>$a = $a * $b</td>
      <td>Multiplication</td>
     </tr>

     <tr>
      <td>$a /= $b</td>
      <td>$a = $a / $b</td>
      <td>Division</td>
     </tr>

     <tr>
      <td>$a %= $b</td>
      <td>$a = $a % $b</td>
      <td>Modulus</td>
     </tr>

     <tr>
      <td>$a **= $b</td>
      <td>$a = $a ** $b</td>
      <td>Exponentiation</td>
     </tr>

    </tbody>
   
  </table>

 </div>

 <div class="sect2" id="language.operators.assignment.bitwise">
  <h3 class="title">Bitwise Assignment Operators</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Example</th>
      <th>Equivalent</th>
      <th>Operation</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>$a &amp;= $b</td>
      <td>$a = $a &amp; $b</td>
      <td>Bitwise And</td>
     </tr>

     <tr>
      <td>$a |= $b</td>
      <td>$a = $a | $b</td>
      <td>Bitwise Or</td>
     </tr>

     <tr>
      <td>$a ^= $b</td>
      <td>$a = $a ^ $b</td>
      <td>Bitwise Xor</td>
     </tr>

     <tr>
      <td>$a &lt;&lt;= $b</td>
      <td>$a = $a &lt;&lt; $b</td>
      <td>Left Shift</td>
     </tr>

     <tr>
      <td>$a &gt;&gt;= $b</td>
      <td>$a = $a &gt;&gt; $b</td>
      <td>Right Shift</td>
     </tr>

    </tbody>
   
  </table>

 </div>

 <div class="sect2" id="language.operators.assignment.other">
  <h3 class="title">Other Assignment Operators</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Example</th>
      <th>Equivalent</th>
      <th>Operation</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>$a .= $b</td>
      <td>$a = $a . $b</td>
      <td>String Concatenation</td>
     </tr>

     <tr>
      <td>$a ??= $b</td>
      <td>$a = $a ?? $b</td>
      <td>Null Coalesce</td>
     </tr>

    </tbody>
   
  </table>

 </div>

 <div class="sect2" id="language.operators.assignment.see-also">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><a href="language.operators.arithmetic.php" class="link">arithmetic operators</a></li>
    <li><a href="language.operators.bitwise.php" class="link">bitwise operators</a></li>
    <li><a href="language.operators.comparison.php#language.operators.comparison.coalesce" class="link">null coalescing operator</a></li>
   </ul>
  </p>
 </div>
</div><?php manual_footer($setup); ?>