<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'language.operators.assignment.php',
    1 => 'Affectation',
    2 => 'Les op&eacute;rateurs d\'affectation',
  ),
  'up' => 
  array (
    0 => 'language.operators.php',
    1 => 'Les op&eacute;rateurs',
  ),
  'prev' => 
  array (
    0 => 'language.operators.increment.php',
    1 => 'Incr&eacute;mentation et d&eacute;cr&eacute;mentation',
  ),
  'next' => 
  array (
    0 => 'language.operators.bitwise.php',
    1 => 'Bitwise',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    '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">Les opérateurs d&#039;affectation</h2>
 
 <p class="simpara">
  L&#039;opérateur d&#039;affectation le plus simple est le signe &quot;=&quot;.
  Le premier réflexe est de penser que ce signe veut dire
  &quot;égal à&quot;. Ce n&#039;est pas le cas. Il signifie que
  l&#039;opérande de gauche se voit affecter la valeur de
  l&#039;expression qui est à droite du signe égal.
 </p>
 <p class="para">
  La valeur d&#039;une expression d&#039;affectation est la valeur
  affectée. Par exemple, la valeur de l&#039;expression
  &#039;<code class="literal">$a = 3</code>&#039; est la valeur 3. Cela permet d&#039;utiliser
  des astuces telles que :
  <div class="example" id="example-1">
   <p><strong>Exemple #1 Affectations imbriquées</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">;<br /></span><span style="color: #FF8000">// $a est maintenant égal à 9, et $b vaut 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">
  En plus du simple opérateur d&#039;affectation, il existe des
  &quot;opérateurs combinés&quot; pour tous les opérateurs
  <a href="language.operators.php" class="link">arithmétiques</a>,
  l&#039;union de tableaux et pour les opérateurs sur les chaînes de caractères.
  Cela permet d&#039;utiliser la valeur d&#039;une variable dans une expression et
  d&#039;affecter le résultat de cette expression à cette variable.
  Par exemple :
  <div class="example" id="example-2">
   <p><strong>Exemple #2 Affectations Combinées</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">// affecte la valeur 8 à la variable $a, correspond à l'instruction '$a = $a + 5';<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #DD0000">"Bonjour "</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">.= </span><span style="color: #DD0000">" tout le monde!"</span><span style="color: #007700">;  </span><span style="color: #FF8000">// affecte la valeur "Bonjour tout le monde!" à<br />                          //  la variable $b<br />                          //  identique à $b = $b." tout le monde!";<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">
  On peut noter que l&#039;affectation copie le contenu de la variable originale
  dans la nouvelle variable (affectation par valeur), ce qui fait que les
  changements de valeur d&#039;une variable ne modifieront pas la valeur de
  l&#039;autre. Cela peut se révéler important lors de la copie d&#039;un grand tableau
  durant une boucle.
 </p>
 <p class="para">
  Une exception au comportement d&#039;affectation par valeur en PHP est le type
  <span class="type"><a href="language.types.object.php" class="type object">object</a></span>, ceux-ci sont affectés par référence.
  La copie d&#039;objet doit être explicitement demandée grâce au mot-clé
  <a href="language.oop5.cloning.php" class="link">clone</a>.
 </p>

 <div class="sect2" id="language.operators.assignment.reference">
  <h3 class="title">Affectation par référence</h3>
  <p class="para">
   L&#039;affectation par référence est aussi supportée, au moyen de la syntaxe
   &quot;<span class="computeroutput">$var = &amp;$othervar;</span>&quot;. L&#039;affectation par
   référence signifie que les deux variables pointent vers le même conteneur de
   donnée, rien n&#039;est copié nulle part.
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Exemple #3 Affectation par référence</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 est une référence à $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">// affiche 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">// affiche 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">// affiche 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">// affiche 4 aussi, car $b est une référence à $a, qui a été<br />              // changée<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   L&#039;opérateur <a href="language.oop5.basic.php#language.oop5.basic.new" class="link">new</a>
   retourne une référence automatiquement, de ce fait, assigner le résultat de
   <a href="language.oop5.basic.php#language.oop5.basic.new" class="link">new</a> par référence est une erreur
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>Exemple #4 Nouvel opérateur par référence</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>L&#039;exemple ci-dessus va afficher :</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">
   Plus d&#039;informations sur les références et leurs utilisations possibles peuvent être
   trouvées dans la section du manuel <a href="language.references.php" class="link">Les références
   expliquées</a>.
  </p>
 </div>

 <div class="sect2" id="language.operators.assignment.arithmetic">
  <h3 class="title">Les opérateurs d&#039;affectation arithmétiques</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Exemple</th>
      <th>Equivalent</th>
      <th>Opération</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>Soustraction</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>Modulo</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">Opérateurs d&#039;affectation bits à bits</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Exemple</th>
      <th>Equivalent</th>
      <th>Opération</th>
     </tr>

    </thead>

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

     <tr>
      <td>$a |= $b</td>
      <td>$a = $a | $b</td>
      <td>Opérateur Or</td>
     </tr>

     <tr>
      <td>$a ^= $b</td>
      <td>$a = $a ^ $b</td>
      <td>Opérateur Xor</td>
     </tr>

     <tr>
      <td>$a &lt;&lt;= $b</td>
      <td>$a = $a &lt;&lt; $b</td>
      <td>Décalage à gauche</td>
     </tr>

     <tr>
      <td>$a &gt;&gt;= $b</td>
      <td>$a = $a &gt;&gt; $b</td>
      <td>Décalage à droite</td>
     </tr>

    </tbody>
   
  </table>

 </div>

 <div class="sect2" id="language.operators.assignment.other">
  <h3 class="title">Autres opérateurs d&#039;affectation</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Exemple</th>
      <th>Equivalent</th>
      <th>Opération</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>$a .= $b</td>
      <td>$a = $a . $b</td>
      <td>Concaténation d&#039;une chaîne de caractères</td>
     </tr>

     <tr>
      <td>$a ??= $b</td>
      <td>$a = $a ?? $b</td>
      <td>Opérateur de coalescence nul</td>
     </tr>

    </tbody>
   
  </table>

 </div>

 <div class="sect2" id="language.operators.assignment.see-also">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><a href="language.operators.arithmetic.php" class="link">les opérateurs arithmétiques</a></li>
    <li><a href="language.operators.bitwise.php" class="link">les opérateurs bits à bits</a></li>
    <li><a href="language.operators.comparison.php#language.operators.comparison.coalesce" class="link">les opérateurs de coalescence nul</a></li>
   </ul>
  </p>
 </div>
</div><?php manual_footer($setup); ?>