<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'language.types.string.php',
    1 => 'Cha&icirc;nes',
    2 => 'Cha&icirc;nes',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Les types',
  ),
  'prev' => 
  array (
    0 => 'language.types.float.php',
    1 => 'Nombres &agrave; virgule flottante',
  ),
  'next' => 
  array (
    0 => 'language.types.numeric-strings.php',
    1 => 'Cha&icirc;nes num&eacute;riques',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'language/types/string.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.types.string" class="sect1">
 <h2 class="title">Chaînes</h2>

 <p class="para">
  Une <span class="type">chaîne</span> est une série de caractères, où un caractère est
  le même qu&#039;un octet. Cela signifie que PHP ne prend en charge qu&#039;un ensemble
  de 256 caractères, et n&#039;offre donc pas de prise en charge native de l&#039;Unicode. Voir
  <a href="language.types.string.php#language.types.string.details" class="link">les détails du type chaîne</a>.
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <span class="simpara">
   Sur les versions 32 bits, une <span class="type">chaîne</span> peut être aussi grande que 2 Go
   (2147483647 octets maximum)
  </span>
 </p></blockquote>

 <div class="sect2" id="language.types.string.syntax">
  <h3 class="title">Syntaxe</h3>

  <p class="para">
   Une <span class="type">chaîne</span> littérale peut être spécifiée de quatre manières différentes :
  </p>

  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.string.php#language.types.string.syntax.single" class="link">entre guillemets simples</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.string.php#language.types.string.syntax.double" class="link">entre guillemets doubles</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.string.php#language.types.string.syntax.heredoc" class="link">syntaxe heredoc</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.string.php#language.types.string.syntax.nowdoc" class="link">syntaxe nowdoc</a>
    </span>
   </li>
  </ul>

  <div class="sect3" id="language.types.string.syntax.single">
   <h4 class="title">Entre guillemets simples</h4>

   <p class="para">
    La manière la plus simple de spécifier une <span class="type">chaîne</span> est de l&#039;encadrer de guillemets
    simples (le caractère <code class="literal">&#039;</code>).
   </p>

   <p class="para">
    Pour spécifier un guillemet simple littéral, échappez-le avec un antislash
    (<code class="literal">\</code>). Pour spécifier un antislash littéral, doublez-le
    (<code class="literal">\\</code>). Toutes les autres occurrences de l&#039;antislash seront traitées
    comme un antislash littéral : cela signifie que les autres séquences d&#039;échappement que l&#039;on
    pourrait connaître, telles que <code class="literal">\r</code> ou <code class="literal">\n</code>,
    seront sorties littéralement comme spécifié plutôt que d&#039;avoir une signification spéciale.
   </p>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     Contrairement aux <a href="language.types.string.php#language.types.string.syntax.double" class="link">guillemets doubles</a>
     et <a href="language.types.string.php#language.types.string.syntax.heredoc" class="link">syntaxes heredoc</a>,
     les <a href="language.variables.php" class="link">variables</a> et les séquences d&#039;échappement
     pour les caractères spéciaux <em>ne</em> seront <em>pas</em> étendues
     lorsqu&#039;elles se trouvent dans des <span class="type">chaînes</span> entre guillemets simples.
    </span>
   </p></blockquote>

   <div class="example" id="example-1">
    <p><strong>Exemple #1 Variantes de syntaxe</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">echo </span><span style="color: #DD0000">'ceci est une chaîne simple'</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">'Vous pouvez également avoir des nouvelles intégrées dans<br />les chaînes de cette manière, car c'</span><span style="color: #0000BB">est<br />acceptable de le faire</span><span style="color: #007700">.</span><span style="color: #DD0000">', PHP_EOL;<br /><br />// Affiche : Arnold a dit un jour : "Je reviendrai"<br />echo '</span><span style="color: #0000BB">Arnold a dit un jour </span><span style="color: #007700">: </span><span style="color: #DD0000">"Je reviendrai"', PHP_EOL;<br /><br />// Affiche : Vous avez supprimé C:\*.* ?<br />echo '</span><span style="color: #0000BB">Vous avez supprimé C</span><span style="color: #007700">:\\*.* ?</span><span style="color: #DD0000">', PHP_EOL;<br /><br />// Affiche : Vous avez supprimé C:\*.* ?<br />echo '</span><span style="color: #0000BB">Vous avez supprimé C</span><span style="color: #007700">:\*.* ?</span><span style="color: #DD0000">', PHP_EOL;<br /><br />// Affiche : Cela ne s'</span><span style="color: #0000BB">étendra pas </span><span style="color: #007700">: </span><span style="color: #0000BB">\n une nouvelle ligne<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Cela ne s\'étendra pas : \n une nouvelle ligne'</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Affiche : Les variables ne $s\'étendent pas $non plus<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Les variables ne $s\'étendent pas $non plus'</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

  </div>

  <div class="sect3" id="language.types.string.syntax.double">
   <h4 class="title">Entre guillemets doubles</h4>

   <p class="para">
    Si la <span class="type">chaîne</span> est encadrée de guillemets doubles (<code class="literal">&quot;</code>), PHP interprétera
    les séquences d&#039;échappement suivantes pour les caractères spéciaux :
   </p>

   <table class="doctable table">
    <caption><strong>Caractères échappés</strong></caption>

    
     <thead>
      <tr>
       <th>Séquence</th>
       <th>Signification</th>
      </tr>

     </thead>


     <tbody class="tbody">
      <tr>
       <td><code class="literal">\n</code></td>
       <td>retour à la ligne (LF ou 0x0A (10) en ASCII)</td>
      </tr>

      <tr>
       <td><code class="literal">\r</code></td>
       <td>retour chariot (CR ou 0x0D (13) en ASCII)</td>
      </tr>

      <tr>
       <td><code class="literal">\t</code></td>
       <td>tabulation horizontale (HT ou 0x09 (9) en ASCII)</td>
      </tr>

      <tr>
       <td><code class="literal">\v</code></td>
       <td>tabulation verticale (VT ou 0x0B (11) en ASCII)</td>
      </tr>

      <tr>
       <td><code class="literal">\e</code></td>
       <td>échappement (ESC ou 0x1B (27) en ASCII)</td>
      </tr>

      <tr>
       <td><code class="literal">\f</code></td>
       <td>saut de page (FF ou 0x0C (12) en ASCII)</td>
      </tr>

      <tr>
       <td><code class="literal">\\</code></td>
       <td>antislash</td>
      </tr>

      <tr>
       <td><code class="literal">\$</code></td>
       <td>signe dollar</td>
      </tr>

      <tr>
       <td><code class="literal">\&quot;</code></td>
       <td>guillemet double</td>
      </tr>

      <tr>
       <td><code class="literal">\[0-7]{1,3}</code></td>
       <td>
        Octal : la séquence de caractères correspondant à l&#039;expression régulière <code class="literal">[0-7]{1,3}</code>
        est un caractère en notation octale (par exemple, <code class="literal">&quot;\101&quot; === &quot;A&quot;</code>),
        qui déborde silencieusement pour s&#039;adapter à un octet (par exemple, <code class="literal">&quot;\400&quot; === &quot;\000&quot;</code>)
       </td>
      </tr>

      <tr>
       <td><code class="literal">\x[0-9A-Fa-f]{1,2}</code></td>
       <td>
        Hexadécimal : la séquence de caractères correspondant à l&#039;expression régulière
        <code class="literal">[0-9A-Fa-f]{1,2}</code> est un caractère en notation hexadécimale
        (par exemple, <code class="literal">&quot;\x41&quot; === &quot;A&quot;</code>)
       </td>
      </tr>

      <tr>
       <td><code class="literal">\u{[0-9A-Fa-f]+}</code></td>
       <td>
        Unicode : la séquence de caractères correspondant à l&#039;expression régulière <code class="literal">[0-9A-Fa-f]+</code>
        est un point de code Unicode, qui sera sorti dans la chaîne sous la représentation UTF-8 de ce point de code.
        Les accolades sont requises dans la séquence. Par exemple, <code class="literal">&quot;\u{41}&quot; === &quot;A&quot;</code>
       </td>
      </tr>

     </tbody>
    
   </table>


   <p class="para">
    Comme pour les <span class="type">chaînes</span> entre guillemets simples, échapper tout autre caractère
    entraînera également l&#039;impression de l&#039;antislash.
   </p>

   <p class="para">
    La caractéristique la plus importante des <span class="type">chaînes</span> entre guillemets doubles est le fait
    que les noms de variables seront étendus. Voir
    <a href="language.types.string.php#language.types.string.parsing" class="link">l&#039;interpolation de chaînes</a> pour
    plus de détails.
   </p>
  </div>

  <div class="sect3" id="language.types.string.syntax.heredoc">
   <h4 class="title">Heredoc</h4>

   <p class="simpara">
    Une troisième manière de délimiter les <span class="type">chaînes</span> est la syntaxe heredoc :
    <code class="literal">&lt;&lt;&lt;</code>. Après cet opérateur, un identifiant est
    fourni, puis une nouvelle ligne. La <span class="type">chaîne</span> elle-même suit, puis
    le même identifiant à nouveau pour fermer la citation.
   </p>

   <p class="simpara">
    L&#039;identifiant de fermeture peut être indenté par des espaces ou des tabulations, auquel cas
    l&#039;indentation sera supprimée de toutes les lignes dans la chaîne doc.
    Avant PHP 7.3.0, l&#039;identifiant de fermeture <em>doit</em>
    commencer dans la première colonne de la ligne.
   </p>

   <p class="simpara">
    De plus, l&#039;identifiant de fermeture doit suivre les mêmes règles de nommage que tout
    autre label en PHP : il doit contenir uniquement des caractères alphanumériques et
    des soulignés, et doit commencer par un caractère non numérique ou un souligné.
   </p>

   <div class="example" id="example-2">
    <p><strong>Exemple #2 Exemple de base de Heredoc à partir de PHP 7.3.0</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: #FF8000">// pas d'indentation<br /></span><span style="color: #007700">echo &lt;&lt;&lt;END<br /></span><span style="color: #DD0000">      a<br />     b<br />    c<br />\n<br /></span><span style="color: #007700">END;<br /><br /></span><span style="color: #FF8000">// 4 espaces d'indentation<br /></span><span style="color: #007700">echo &lt;&lt;&lt;END<br /></span><span style="color: #DD0000">      a<br />     b<br />    c<br /></span><span style="color: #007700">    END;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus en PHP 7.3 :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
      a
     b
    c

  a
 b
c
</pre></div>
    </div>
   </div>

   <p class="simpara">
    Si l&#039;identifiant de fermeture est indenté plus que n&#039;importe quelle ligne du corps, alors une <span class="classname"><a href="class.parseerror.php" class="classname">ParseError</a></span> sera levée :
   </p>

   <div class="example" id="example-3">
    <p><strong>Exemple #3 L&#039;identifiant de fermeture ne doit pas être indenté plus que n&#039;importe quelle ligne du corps</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">echo &lt;&lt;&lt;END<br /></span><span style="color: #DD0000">  a<br /> b<br />c<br /></span><span style="color: #007700">   END;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus en PHP 7.3 :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Parse error: Invalid body indentation level (expecting an indentation level of at least 3) in example.php on line 4
</pre></div>
    </div>
   </div>

   <p class="simpara">
    Si l&#039;identifiant de fermeture est indenté, des tabulations peuvent également être utilisées, cependant,
    les tabulations et les espaces <em>ne doivent pas</em> être mélangés concernant
    l&#039;indentation de l&#039;identifiant de fermeture et l&#039;indentation du corps
    (jusqu&#039;à l&#039;identifiant de fermeture). Dans l&#039;un de ces cas, une <span class="classname"><a href="class.parseerror.php" class="classname">ParseError</a></span> sera levée.

    Ces contraintes d&#039;espace ont été incluses car le mélange d&#039;espaces et de tabulations pour l&#039;indentation nuit à la lisibilité.
   </p>

   <div class="example" id="example-4">
    <p><strong>Exemple #4 Indentation différente pour le corps (espaces) identifiant de fermeture</strong></p>
    <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Tout le code suivant ne fonctionne pas.<br /><br />// indentation différente pour le corps (espaces) marqueur de fin (tabulations)<br /></span><span style="color: #007700">{<br />    echo &lt;&lt;&lt;END<br /></span><span style="color: #DD0000">     a<br /></span><span style="color: #007700">        END;<br />}<br /><br /></span><span style="color: #FF8000">// mélange d'espaces et de tabulations dans le corps<br /></span><span style="color: #007700">{<br />    echo &lt;&lt;&lt;END<br /></span><span style="color: #DD0000">        a<br /></span><span style="color: #007700">     END;<br />}<br /><br /></span><span style="color: #FF8000">// mélange d'espaces et de tabulations dans le marqueur de fin<br /></span><span style="color: #007700">{<br />    echo &lt;&lt;&lt;END<br /></span><span style="color: #DD0000">          a<br /></span><span style="color: #007700">         END;<br />}</span></span></code></div>
    </div>

    <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus en PHP 7.3 :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Parse error: Invalid indentation - tabs and spaces cannot be mixed in example.php line 8
</pre></div>
    </div>
   </div>

   <p class="simpara">
    L&#039;identifiant de fermeture pour la chaîne du corps n&#039;est pas requis pour être
    suivi d&#039;un point-virgule ou d&#039;un saut de ligne. Par exemple, le code suivant
    est autorisé à partir de PHP 7.3.0 :
   </p>

   <div class="example" id="example-5">
    <p><strong>Exemple #5 Continuation d&#039;une expression après un identifiant de fermeture</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$values </span><span style="color: #007700">= [&lt;&lt;&lt;END<br /></span><span style="color: #DD0000">a<br />  b<br />    c<br /></span><span style="color: #007700">END, </span><span style="color: #DD0000">'d e f'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$values</span><span style="color: #007700">);</span></span></code></div>
    </div>

    <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus en PHP 7.3 :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
array(2) {
  [0] =&gt;
  string(11) &quot;a
  b
    c&quot;
  [1] =&gt;
  string(5) &quot;d e f&quot;
}
</pre></div>
    </div>
   </div>

   <div class="warning"><strong class="warning">Avertissement</strong>
    <p class="simpara">
     Si l&#039;identifiant de fermeture a été trouvé au début d&#039;une ligne, alors
     peu importe s&#039;il faisait partie d&#039;un autre mot, il peut être considéré
     comme l&#039;identifiant de fermeture et provoquer une <span class="classname"><a href="class.parseerror.php" class="classname">ParseError</a></span>.
    </p>

    <div class="example" id="example-6">
     <p><strong>Exemple #6 L&#039;identifiant de fermeture dans le corps de la chaîne tend à provoquer une ParseError</strong></p>
     <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$values </span><span style="color: #007700">= [&lt;&lt;&lt;END<br /></span><span style="color: #DD0000">a<br />b<br /></span><span style="color: #007700">END </span><span style="color: #0000BB">ING<br />END</span><span style="color: #007700">, </span><span style="color: #DD0000">'d e f'</span><span style="color: #007700">];</span></span></code></div>
     </div>

     <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus en PHP 7.3 :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Parse error: syntax error, unexpected identifier &quot;ING&quot;, expecting &quot;]&quot; in example.php on line 5
</pre></div>
     </div>
    </div>

    <p class="simpara">
     Pour éviter ce problème, il est sûr de suivre la règle simple :
     <em>ne pas choisir un mot qui apparaît dans le corps du texte
     en tant qu&#039;identifiant de fermeture</em>.
    </p>

   </div>

   <div class="warning"><strong class="warning">Avertissement</strong>
    <p class="simpara">
     Avant PHP 7.3.0, il est très important de noter que la ligne contenant l&#039;
     identifiant de fermeture ne doit contenir aucun autre caractère, sauf un point-virgule
     (<code class="literal">;</code>).
     Cela signifie surtout que l&#039;identifiant
     <em>ne peut pas être indenté</em>, et il ne doit pas y avoir d&#039;espaces
     ou de tabulations avant ou après le point-virgule. Il est également important de réaliser que
     le premier caractère avant l&#039;identifiant de fermeture doit être un saut de ligne tel
     que défini par le système d&#039;exploitation local. C&#039;est <code class="literal">\n</code> sur
     les systèmes UNIX, y compris macOS. Le délimiteur de fermeture doit également être
     suivi d&#039;un saut de ligne.
    </p>

    <p class="simpara">
     Si cette règle est enfreinte et que l&#039;identifiant de fermeture n&#039;est pas &quot;propre&quot;, il ne sera
     pas considéré comme un identifiant de fermeture, et PHP continuera à en chercher un. Si un
     identifiant de fermeture approprié n&#039;est pas trouvé avant la fin du fichier
     courant, une erreur d&#039;analyse se produira à la dernière ligne.
    </p>

    <div class="example" id="example-7">
     <p><strong>Exemple #7 Exemple invalide, avant PHP 7.3.0</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">foo </span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$bar </span><span style="color: #007700">= &lt;&lt;&lt;EOT<br /></span><span style="color: #DD0000">bar<br /></span><span style="color: #007700">    EOT;<br />}<br /></span><span style="color: #FF8000">// L'identifiant ne doit pas être indenté<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
    <div class="example" id="example-8">
     <p><strong>Exemple #8 Exemple valide, même avant PHP 7.3.0</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">foo </span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$bar </span><span style="color: #007700">= &lt;&lt;&lt;EOT<br /></span><span style="color: #DD0000">bar<br /></span><span style="color: #007700">EOT;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>

    <p class="para">
     Les heredocs contenant des variables ne peuvent pas être utilisés pour initialiser des propriétés de classe.
    </p>

   </div>

   <p class="para">
    Le texte heredoc se comporte exactement comme une <span class="type"><a href="language.types.string.php" class="type string">string</a></span> entre guillemets doubles, sans les guillemets. Cela signifie que les guillemets dans un heredoc n&#039;ont pas besoin d&#039;être échappés, mais les codes d&#039;échappement mentionnés ci-dessus peuvent toujours être utilisés. Les variables sont développées, mais il faut prendre le même soin lors de l&#039;expression de variables complexes à l&#039;intérieur d&#039;un heredoc que pour les <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s.
   </p>

   <div class="example" id="example-9">
    <p><strong>Exemple #9 Exemple de citation de chaîne heredoc</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$str </span><span style="color: #007700">= &lt;&lt;&lt;EOD<br /></span><span style="color: #DD0000">Exemple de chaîne<br />s'étendant sur plusieurs lignes<br />utilisant la syntaxe heredoc.<br /></span><span style="color: #007700">EOD;<br /><br /></span><span style="color: #FF8000">/* Exemple plus complexe, avec des variables. */<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">foo<br /></span><span style="color: #007700">{<br />    var </span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br />    var </span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /><br />    function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo </span><span style="color: #007700">= </span><span style="color: #DD0000">'Foo'</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bar </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'Bar1'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Bar2'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Bar3'</span><span style="color: #007700">);<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'MyName'</span><span style="color: #007700">;<br /><br />echo &lt;&lt;&lt;EOT<br /></span><span style="color: #DD0000">Mon nom est "</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">". Je suis en train d'imprimer </span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #DD0000">.<br />Maintenant, j'imprime </span><span style="color: #007700">{</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bar</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]}</span><span style="color: #DD0000">.<br />Cela devrait imprimer un 'A' majuscule : \x41<br /></span><span style="color: #007700">EOT;<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>
Mon nom est &quot;MyName&quot;. Je suis en train d&#039;imprimer Foo.
Maintenant, j&#039;imprime Bar2.
Cela devrait imprimer un &#039;A&#039; majuscule : A</pre></div>
    </div>
   </div>

   <p class="para">
    Il est également possible d&#039;utiliser la syntaxe heredoc pour passer des données aux arguments de fonction :
   </p>

   <div class="example" id="example-10">
    <p><strong>Exemple #10 Heredoc dans les exemples d&#039;arguments</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">(array(&lt;&lt;&lt;EOD<br /></span><span style="color: #DD0000">foobar!<br /></span><span style="color: #007700">EOD<br />));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Il est possible d&#039;initialiser des variables statiques et des propriétés/constants de classe en utilisant la syntaxe heredoc :
   </p>

   <div class="example" id="example-11">
    <p><strong>Exemple #11 Utilisation d&#039;Heredoc pour initialiser des valeurs statiques</strong></p>
    <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Variables statiques<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />{<br />    static </span><span style="color: #0000BB">$bar </span><span style="color: #007700">= &lt;&lt;&lt;LABEL<br /></span><span style="color: #DD0000">Rien ici...<br /></span><span style="color: #007700">LABEL;<br />}<br /><br /></span><span style="color: #FF8000">// Propriétés/constants de classe<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">foo<br /></span><span style="color: #007700">{<br />    const </span><span style="color: #0000BB">BAR </span><span style="color: #007700">= &lt;&lt;&lt;FOOBAR<br /></span><span style="color: #DD0000">Exemple de constante<br /></span><span style="color: #007700">FOOBAR;<br /><br />    public </span><span style="color: #0000BB">$baz </span><span style="color: #007700">= &lt;&lt;&lt;FOOBAR<br /></span><span style="color: #DD0000">Exemple de propriété<br /></span><span style="color: #007700">FOOBAR;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    L&#039;identifiant d&#039;ouverture du Heredoc peut éventuellement être
    encadré de guillemets doubles :
   </p>

   <div class="example" id="example-12">
    <p><strong>Exemple #12 Utilisation de guillemets doubles dans le Heredoc</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">echo &lt;&lt;&lt;"FOOBAR"<br /></span><span style="color: #DD0000">Bonjour le monde !<br /></span><span style="color: #007700">FOOBAR;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

  </div>

  <div class="sect3" id="language.types.string.syntax.nowdoc">
   <h4 class="title">Nowdoc</h4>

   <p class="para">
    Les nowdocs sont aux chaînes entre guillemets simples ce que les heredocs sont aux chaînes entre guillemets doubles. Un nowdoc est spécifié de manière similaire à un heredoc, mais <em>aucune interpolation de chaîne n&#039;est effectuée</em> à l&#039;intérieur d&#039;un nowdoc. La construction est idéale pour intégrer du code PHP ou d&#039;autres blocs de texte volumineux sans avoir besoin d&#039;échapper. Il partage certaines caractéristiques avec la construction SGML
    <code class="literal">&lt;![CDATA[ ]]&gt;</code>, en ce sens qu&#039;elle déclare un
    bloc de texte qui n&#039;est pas destiné à être analysé.
   </p>

   <p class="para">
    Un nowdoc est identifié par la même séquence <code class="literal">&lt;&lt;&lt;</code>
    utilisée pour les heredocs, mais l&#039;identifiant qui suit est encadré de guillemets simples, par exemple <code class="literal">&lt;&lt;&lt;&#039;EOT&#039;</code>. Toutes les règles pour les identifiants heredoc s&#039;appliquent également aux identifiants nowdoc, en particulier celles concernant l&#039;apparence de l&#039;identifiant de fermeture.
   </p>

   <div class="example" id="example-13">
    <p><strong>Exemple #13 Exemple de citation de chaîne nowdoc</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">echo &lt;&lt;&lt;'EOD'<br /></span><span style="color: #DD0000">Exemple de chaîne s'étendant sur plusieurs lignes<br />utilisant la syntaxe nowdoc. Les barres obliques inverses sont toujours traitées littéralement,<br />c'est-à-dire \\ et \'.<br /></span><span style="color: #007700">EOD;</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>
Exemple de chaîne s&#039;étendant sur plusieurs lignes
utilisant la syntaxe nowdoc. Les barres obliques inverses sont toujours traitées littéralement,
c&#039;est-à-dire \\ et \&#039;.
</pre></div>
    </div>
   </div>

   <div class="example" id="example-14">
    <p><strong>Exemple #14 Exemple de citation de chaîne nowdoc avec des variables</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">foo<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br />    public </span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /><br />    function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo </span><span style="color: #007700">= </span><span style="color: #DD0000">'Foo'</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bar </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'Bar1'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Bar2'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Bar3'</span><span style="color: #007700">);<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$name </span><span style="color: #007700">= </span><span style="color: #DD0000">'MyName'</span><span style="color: #007700">;<br /><br />echo &lt;&lt;&lt;'EOT'<br /></span><span style="color: #DD0000">Mon nom est "$name". Je suis en train d'imprimer $foo-&gt;foo.<br />Maintenant, j'imprime {$foo-&gt;bar[1]}.<br />Cela ne devrait pas imprimer un 'A' majuscule : \x41<br /></span><span style="color: #007700">EOT;<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>
Mon nom est &quot;$name&quot;. Je suis en train d&#039;imprimer $foo-&gt;foo.
Maintenant, j&#039;imprime {$foo-&gt;bar[1]}.
Cela ne devrait pas imprimer un &#039;A&#039; majuscule : \x41</pre></div>
    </div>
   </div>

   <div class="example" id="example-15">
    <p><strong>Exemple #15 Exemple de données statiques</strong></p>
    <div class="example-contents">
<div class="annotation-non-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">foo </span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$bar </span><span style="color: #007700">= &lt;&lt;&lt;'EOT'<br /></span><span style="color: #DD0000">bar<br /></span><span style="color: #007700">EOT;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

  </div>

  <div class="sect3" id="language.types.string.parsing">
   <h4 class="title">Interpolation de chaînes</h4>

   <p class="simpara">
    Lorsqu&#039;une <span class="type">chaîne</span> est spécifiée entre guillemets doubles ou avec heredoc,
    des <a href="language.variables.php" class="link">variables</a> peuvent être substituées à l&#039;intérieur.
   </p>

   <p class="simpara">
    Il existe deux types de syntaxe : une
    <a href="language.types.string.php#language.types.string.parsing.basic" class="link">de base</a> et une
    <a href="language.types.string.php#language.types.string.parsing.advanced" class="link">avancée</a>.
    La syntaxe de base est la plus courante et la plus pratique. Elle offre un moyen d&#039;incorporer une variable, une valeur <span class="type">tableau</span> ou une propriété <span class="type">objet</span> dans une <span class="type">chaîne</span> avec un minimum d&#039;effort.
   </p>

   <div class="sect4" id="language.types.string.parsing.basic">
    <h5 class="title">Syntaxe de base</h5>
    <p class="simpara">
     Si un signe dollar (<code class="literal">$</code>) est rencontré, les caractères qui le suivent et qui peuvent être utilisés dans un nom de variable seront interprétés comme tels et substitués.
    </p>
    <div class="example" id="example-16">
     <p><strong>Exemple #16 Interpolation de chaînes</strong></p>
     <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$juice </span><span style="color: #007700">= </span><span style="color: #DD0000">"pomme"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"Il a bu un peu de </span><span style="color: #0000BB">$juice</span><span style="color: #DD0000"> jus." </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><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>
Il a bu un peu de pomme jus.
</pre></div>
     </div>
    </div>

    <p class="simpara">
     Formellement, la structure pour la syntaxe de substitution de variable de base est la suivante :
    </p>
    <div class="informalexample">
     <div class="example-contents">
<div class="annotation-interactive cdata"><pre>
string-variable::
     variable-name   (offset-or-property)?
   | ${   expression   }

offset-or-property::
     offset-in-string
   | property-in-string

offset-in-string::
     [   name   ]
   | [   variable-name   ]
   | [   integer-literal   ]

property-in-string::
     -&gt;  name

variable-name::
     $   name

name::
     [a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*

</pre></div>
     </div>

    </div>

    <div class="warning"><strong class="warning">Avertissement</strong>
     <p class="para">
      La syntaxe <code class="literal">${ expression }</code> est dépréciée depuis
      PHP 8.2.0, car elle peut être interprétée comme
      <a href="language.variables.variable.php" class="link">des variables de variables</a> :
      <div class="informalexample">
       <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">const </span><span style="color: #0000BB">foo </span><span style="color: #007700">= </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #007700">${</span><span style="color: #0000BB">foo</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #007700">${(</span><span style="color: #0000BB">foo</span><span style="color: #007700">)}</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
       </div>

       <p class="para">Résultat de l&#039;exemple ci-dessus en PHP 8.2 :</p>
       <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Deprecated: Using ${var} in strings is deprecated, use {$var} instead in file on line 6

Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in file on line 9
string(3) &quot;foo&quot;
string(3) &quot;bar&quot;
</pre></div>
       </div>
       <p class="para">L&#039;exemple ci-dessus va afficher :</p>
       <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
string(3) &quot;foo&quot;
string(3) &quot;bar&quot;
</pre></div>
       </div>
      </div>
      La syntaxe d&#039;interpolation de chaîne <a href="language.types.string.php#language.types.string.parsing.advanced" class="link">avancée</a> devrait être utilisée à la place.
     </p>
    </div>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      S&#039;il n&#039;est pas possible de former un nom valide, le signe dollar reste
      tel quel dans la chaîne :
     </span>
     <div class="informalexample">
      <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">echo </span><span style="color: #DD0000">"Aucune interpolation $  n'a eu lieu\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Aucune interpolation $\n n'a eu lieu\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Aucune interpolation $2 n'a eu lieu\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

      <p class="para">L&#039;exemple ci-dessus va afficher :</p>
      <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Aucune interpolation $  n&#039;a eu lieu
Aucune interpolation $
 n&#039;a eu lieu
Aucune interpolation $2 n&#039;a eu lieu
</pre></div>
      </div>
    </div>
    </p></blockquote>

    <div class="example" id="example-17">
     <p><strong>Exemple #17 Interpolation de la valeur de la première dimension d&#039;un tableau ou d&#039;une propriété</strong></p>
     <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$juices </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"pomme"</span><span style="color: #007700">, </span><span style="color: #DD0000">"orange"</span><span style="color: #007700">, </span><span style="color: #DD0000">"string_key" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"violet"</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Il a bu un peu de </span><span style="color: #0000BB">$juices</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]</span><span style="color: #DD0000"> jus."</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Il a bu un peu de </span><span style="color: #0000BB">$juices</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]</span><span style="color: #DD0000"> jus."</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Il a bu un peu de </span><span style="color: #0000BB">$juices</span><span style="color: #007700">[</span><span style="color: #0000BB">string_key</span><span style="color: #007700">]</span><span style="color: #DD0000"> jus."</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$s </span><span style="color: #007700">= </span><span style="color: #DD0000">"chaîne"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$o </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">();<br /><br />echo </span><span style="color: #DD0000">"Valeur de l'objet : </span><span style="color: #0000BB">$o</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">s</span><span style="color: #DD0000">."</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>
Il a bu un peu de pomme jus.
Il a bu un peu de orange jus.
Il a bu un peu de violet jus.
Valeur de l&#039;objet : chaîne.
</pre></div>
     </div>
    </div>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      La clé du tableau doit être non citée, et il n&#039;est donc pas possible de
      référencer une constante comme clé avec la syntaxe de base. Utiliser la
      syntaxe <a href="language.types.string.php#language.types.string.parsing.advanced" class="link">avancée</a>
      à la place.
     </span>
    </p></blockquote>

    <p class="simpara">
     À partir de PHP 7.1.0, les indices numériques <em>négatifs</em> sont également
     supportés.
    </p>

    <div class="example" id="example-18"><p><strong>Exemple #18 Indices numériques négatifs</strong></p>
     <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$string </span><span style="color: #007700">= </span><span style="color: #DD0000">'chaîne'</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Le caractère à l'indice -2 est </span><span style="color: #0000BB">$string</span><span style="color: #007700">[-</span><span style="color: #0000BB">2</span><span style="color: #007700">]</span><span style="color: #DD0000">."</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$string</span><span style="color: #007700">[-</span><span style="color: #0000BB">3</span><span style="color: #007700">] = </span><span style="color: #DD0000">'o'</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Changer le caractère à l'indice -3 en o donne </span><span style="color: #0000BB">$string</span><span style="color: #DD0000">."</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</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>
Le caractère à l&#039;indice -2 est n.
Changer le caractère à l&#039;indice -3 en o donne strong.
</pre></div>
     </div>
    </div>

    <p class="simpara">
     Pour tout ce qui est plus complexe, la
     <a href="language.types.string.php#language.types.string.parsing.advanced" class="link">syntaxe avancée</a>
     doit être utilisée.
    </p>
   </div>

   <div class="sect4" id="language.types.string.parsing.advanced">
    <h5 class="title">Syntaxe avancée (syntaxe des accolades)</h5>

    <p class="simpara">
     La syntaxe avancée permet l&#039;interpolation de
     <em>variables</em> avec des accesseurs arbitraires.
    </p>

    <p class="simpara">
     Toute variable scalaire, élément de tableau ou propriété d&#039;objet
     (<span class="modifier">statique</span> ou non) avec une représentation
     <span class="type">chaîne</span> peut être incluse via cette syntaxe.
     L&#039;expression est écrite de la même manière que celle qui apparaîtrait en dehors de la
     <span class="type">chaîne</span>, puis entourée de <code class="literal">{</code> et
     <code class="literal">}</code>. Étant donné que <code class="literal">{</code> ne peut pas être échappé, cette
     syntaxe ne sera reconnue que lorsque le <code class="literal">$</code> suit immédiatement le
     <code class="literal">{</code>. Utiliser <code class="literal">{\$</code> pour obtenir un
     <code class="literal">{$</code>. Voici quelques exemples pour clarifier :
    </p>

     <div class="example" id="example-19">
     <p><strong>Exemple #19 Syntaxe avec des accolades</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">const </span><span style="color: #0000BB">DATA_KEY </span><span style="color: #007700">= </span><span style="color: #DD0000">'const-key'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$great </span><span style="color: #007700">= </span><span style="color: #DD0000">'fantastique'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$arr </span><span style="color: #007700">= [<br />    </span><span style="color: #DD0000">'1'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'2'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'3'</span><span style="color: #007700">,<br />    [</span><span style="color: #0000BB">41</span><span style="color: #007700">, </span><span style="color: #0000BB">42</span><span style="color: #007700">, </span><span style="color: #0000BB">43</span><span style="color: #007700">],<br />    </span><span style="color: #DD0000">'key' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Valeur indexée'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'const-key' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Clé avec un signe moins'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'foo' </span><span style="color: #007700">=&gt; [</span><span style="color: #DD0000">'foo1'</span><span style="color: #007700">, </span><span style="color: #DD0000">'foo2'</span><span style="color: #007700">, </span><span style="color: #DD0000">'foo3'</span><span style="color: #007700">]<br />];<br /><br /></span><span style="color: #FF8000">// Ne fonctionnera pas, affiche : This is { fantastic}<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Ceci est { </span><span style="color: #0000BB">$great</span><span style="color: #DD0000">}" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Fonctionne, affiche : This is fantastic<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Ceci est </span><span style="color: #007700">{</span><span style="color: #0000BB">$great</span><span style="color: #007700">}</span><span style="color: #DD0000">" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Pour afficher des accolades dans la sortie :<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Ceci est {</span><span style="color: #007700">{</span><span style="color: #0000BB">$great</span><span style="color: #007700">}</span><span style="color: #DD0000">}" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />class </span><span style="color: #0000BB">Square </span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$width</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">int $width</span><span style="color: #007700">) { </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">width </span><span style="color: #007700">= </span><span style="color: #0000BB">$width</span><span style="color: #007700">; }<br />}<br /><br /></span><span style="color: #0000BB">$square </span><span style="color: #007700">= new </span><span style="color: #0000BB">Square</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Fonctionne<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Ce carré mesure </span><span style="color: #007700">{</span><span style="color: #0000BB">$square</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">width</span><span style="color: #007700">}</span><span style="color: #DD0000">00 centimètres de large." </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Fonctionne, les clés entre guillemets ne fonctionnent qu'avec la syntaxe des accolades<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Cela fonctionne : </span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'key'</span><span style="color: #007700">]}</span><span style="color: #DD0000">" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Fonctionne<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Cela fonctionne : </span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">3</span><span style="color: #007700">][</span><span style="color: #0000BB">2</span><span style="color: #007700">]}</span><span style="color: #DD0000">" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"Cela fonctionne : </span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">DATA_KEY</span><span style="color: #007700">]}</span><span style="color: #DD0000">" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Lors de l'utilisation de tableaux multidimensionnels, utilisez toujours des accolades autour des tableaux<br />// lorsqu'ils sont à l'intérieur de chaînes<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Cela fonctionne : </span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">][</span><span style="color: #0000BB">2</span><span style="color: #007700">]}</span><span style="color: #DD0000">" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"Cela fonctionne : </span><span style="color: #007700">{</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">values</span><span style="color: #007700">[</span><span style="color: #0000BB">3</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">}</span><span style="color: #DD0000">" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"Cela fonctionne : </span><span style="color: #007700">{</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$staticProp</span><span style="color: #007700">}</span><span style="color: #DD0000">" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Ne fonctionnera pas, affiche : C:\directory\{fantastic}.txt<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"C:\directory\{</span><span style="color: #0000BB">$great</span><span style="color: #DD0000">}.txt" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Fonctionne, affiche : C:\directory\fantastic.txt<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"C:\\directory\\</span><span style="color: #007700">{</span><span style="color: #0000BB">$great</span><span style="color: #007700">}</span><span style="color: #DD0000">.txt" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      Comme cette syntaxe permet des expressions arbitraires, il est possible d&#039;utiliser
      <a href="language.variables.variable.php" class="link">des variables variables</a>
      dans la syntaxe avancée.
     </span>
    </p></blockquote>
   </div>
  </div>

  <div class="sect3" id="language.types.string.substr">
   <h4 class="title">Accès et modification de chaîne par caractère</h4>

   <p class="para">
    Les caractères dans les <span class="type">chaînes</span> peuvent être accédés et modifiés en
    spécifiant l&#039;offset basé sur zéro du caractère souhaité après la
    <span class="type">chaîne</span> à l&#039;aide de crochets <span class="type"><a href="language.types.array.php" class="type array">array</a></span>, comme dans
    <var class="varname">$str[42]</var>. Pensez à une <span class="type">chaîne</span> comme à un
    <span class="type">tableau</span> de caractères à cette fin. Les fonctions
    <span class="function"><a href="function.substr.php" class="function">substr()</a></span> et <span class="function"><a href="function.substr-replace.php" class="function">substr_replace()</a></span>
    peuvent être utilisées pour extraire ou remplacer plus d&#039;un caractère.
   </p>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     À partir de PHP 7.1.0, les offsets de chaîne négatifs sont également supportés. Ceux-ci spécifient
     l&#039;offset à partir de la fin de la chaîne.
     Auparavant, les offsets négatifs émettaient <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> pour la lecture
     (produisant une chaîne vide) et <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> pour l&#039;écriture
     (laissant la chaîne intacte).
    </span>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     Avant PHP 8.0.0, les <span class="type">chaînes</span> pouvaient également être accédées en utilisant des accolades, comme dans
     <var class="varname">$str{42}</var>, pour le même objectif.
     Cette syntaxe des accolades a été dépréciée à partir de PHP 7.4.0 et n&#039;est plus prise en charge à partir de PHP 8.0.0.
    </span>
   </p></blockquote>

   <div class="warning"><strong class="warning">Avertissement</strong>
    <p class="simpara">
     Écrire à un offset hors de portée remplit la chaîne d&#039;espaces.
     Les types non entiers sont convertis en entier.
     Un type d&#039;offset illégal émet <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
     Seul le premier caractère d&#039;une chaîne assignée est utilisé.
     À partir de PHP 7.1.0, assigner une chaîne vide génère une erreur fatale. Auparavant,
     cela assignait un octet NULL.
    </p>
   </div>

   <div class="warning"><strong class="warning">Avertissement</strong>
    <p class="simpara">
     En interne, les chaînes PHP sont des tableaux d&#039;octets. En conséquence, accéder ou
     modifier une chaîne à l&#039;aide de crochets de tableau n&#039;est pas sûr pour les multi-octets, et
     ne devrait être fait qu&#039;avec des chaînes en encodage à un seul octet tel que ISO-8859-1.
    </p>
   </div>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     À partir de PHP 7.1.0, appliquer l&#039;opérateur d&#039;index vide sur une chaîne vide génère une erreur fatale.
     Auparavant, la chaîne vide était silencieusement convertie en tableau.
    </span>
   </p></blockquote>

   <div class="example" id="example-20">
    <p><strong>Exemple #20 Quelques exemples de chaînes</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: #FF8000">// Obtenez le premier caractère d'une chaîne<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'Ceci est un test.'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$first </span><span style="color: #007700">= </span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$first</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Obtenez le troisième caractère d'une chaîne<br /></span><span style="color: #0000BB">$third </span><span style="color: #007700">= </span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$third</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Obtenez le dernier caractère d'une chaîne.<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'Ceci est toujours un test.'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$last </span><span style="color: #007700">= </span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">)-</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$last</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Modifiez le dernier caractère d'une chaîne<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'Regardez la mer'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">)-</span><span style="color: #0000BB">1</span><span style="color: #007700">] = </span><span style="color: #DD0000">'e'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    Les offsets de chaîne doivent être des entiers ou des chaînes ressemblant à des entiers,
    sinon un avertissement sera émis.
   </p>

   <div class="example" id="example-21">
    <p><strong>Exemple #21 Exemple d&#039;offsets de chaîne illégaux</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'abc'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$keys </span><span style="color: #007700">= [ </span><span style="color: #DD0000">'1'</span><span style="color: #007700">, </span><span style="color: #DD0000">'1.0'</span><span style="color: #007700">, </span><span style="color: #DD0000">'x'</span><span style="color: #007700">, </span><span style="color: #DD0000">'1x' </span><span style="color: #007700">];<br /><br />foreach (</span><span style="color: #0000BB">$keys </span><span style="color: #007700">as </span><span style="color: #0000BB">$keyToTry</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(isset(</span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">$keyToTry</span><span style="color: #007700">]));<br /><br />    try {<br />        </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">$keyToTry</span><span style="color: #007700">]);<br />    } catch (</span><span style="color: #0000BB">TypeError $e</span><span style="color: #007700">) {<br />        echo </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    }<br /><br />    echo </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<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>
bool(true)
string(1) &quot;b&quot;

bool(false)
Cannot access offset of type string on string

bool(false)
Cannot access offset of type string on string

bool(false)

Warning: Illegal string offset &quot;1x&quot; in Standard input code on line 10
string(1) &quot;b&quot;
</pre></div>
    </div>
   </div>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Accéder à des variables d&#039;autres types (à l&#039;exception des tableaux ou objets
     implémentant les interfaces appropriées) en utilisant <code class="literal">[]</code> ou
     <code class="literal">{}</code> renvoie silencieusement <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
    </p>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Les caractères dans les littéraux de chaîne peuvent être accédés
     en utilisant <code class="literal">[]</code> ou <code class="literal">{}</code>.
    </p>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Accéder à des caractères dans des littéraux de chaîne en utilisant la
     syntaxe <code class="literal">{}</code> a été déprécié dans PHP 7.4.
     Cela a été supprimé dans PHP 8.0.
    </p>
   </p></blockquote>
  </div>
 </div>

 <div class="sect2" id="language.types.string.useful-funcs">
  <h3 class="title">Fonctions et opérateurs utiles</h3>

  <p class="para">
   Les <span class="type">chaînes</span> peuvent être concaténées à l&#039;aide de l&#039;opérateur &#039;.&#039; (point). Il est à noter
   que l&#039;opérateur &#039;+&#039; (addition) ne fonctionnera <em>pas</em> pour cela.
   Consulter <a href="language.operators.string.php" class="link">les opérateurs de chaîne</a> pour
   plus d&#039;informations.
  </p>

  <p class="para">
   Il existe plusieurs fonctions utiles pour la manipulation des <span class="type">chaînes</span>.
  </p>

  <p class="simpara">
   Consulter la <a href="ref.strings.php" class="link">section des fonctions de chaîne</a> pour
   les fonctions générales, et la <a href="ref.pcre.php" class="link">section des fonctions d&#039;expressions régulières compatibles Perl</a> pour
   des fonctionnalités avancées de recherche et de remplacement.
  </p>

  <p class="simpara">
   Il existe également des <a href="ref.url.php" class="link">fonctions pour les chaînes d&#039;URL</a>, et
   des fonctions pour chiffrer/déchiffrer des chaînes
   (<a href="ref.sodium.php" class="link">Sodium</a> et
   <a href="ref.hash.php" class="link">Hash</a>).
  </p>

  <p class="simpara">
   Enfin, consulter également les <a href="ref.ctype.php" class="link">fonctions de type caractère</a>.
  </p>
 </div>

 <div class="sect2" id="language.types.string.casting">
  <h3 class="title">Conversion en chaîne</h3>

  <p class="para">
   Une valeur peut être convertie en <span class="type">chaîne</span> à l&#039;aide du
   cast <code class="literal">(string)</code> ou de la fonction <span class="function"><a href="function.strval.php" class="function">strval()</a></span>.
   La conversion en <span class="type">chaîne</span> est effectuée automatiquement dans le contexte d&#039;une
   expression où une <span class="type">chaîne</span> est nécessaire. Cela se produit lors de l&#039;utilisation des
   fonctions <span class="function"><a href="function.echo.php" class="function">echo</a></span> ou <span class="function"><a href="function.print.php" class="function">print</a></span>, ou lorsque
   une variable est comparée à une <span class="type">chaîne</span>. Les sections sur
   <a href="language.types.php" class="link">Types</a> et
   <a href="language.types.type-juggling.php" class="link">Type Juggling</a> clarifieront
   ce qui suit. Voir également la fonction <span class="function"><a href="function.settype.php" class="function">settype()</a></span>.
  </p>

  <p class="para">
   Une valeur <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> est convertie en la <span class="type">chaîne</span>
   <code class="literal">&quot;1&quot;</code>. La <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> est convertie en
   <code class="literal">&quot;&quot;</code> (la chaîne vide). Cela permet une conversion aller-retour entre
   les valeurs <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> et <span class="type">chaîne</span>.
  </p>

  <p class="para">
   Un <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> ou <span class="type"><a href="language.types.float.php" class="type float">float</a></span> est converti en une
   <span class="type">chaîne</span> représentant le nombre textuellement (y compris la
   partie exponentielle pour les <span class="type"><a href="language.types.float.php" class="type float">float</a></span>). Les nombres à virgule flottante peuvent être
   convertis à l&#039;aide de la notation exponentielle (<code class="literal">4.1E+6</code>).
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    À partir de PHP 8.0.0, le caractère de la virgule décimale est toujours
    un point (&quot;<code class="literal">.</code>&quot;). Avant PHP 8.0.0,
    le caractère de la virgule décimale est défini dans la locale du script (catégorie
    LC_NUMERIC). Consulter la fonction <span class="function"><a href="function.setlocale.php" class="function">setlocale()</a></span>.
   </p>
  </p></blockquote>

  <p class="para">
   Les <span class="type">tableaux</span> sont toujours convertis en la <span class="type">chaîne</span>
   <code class="literal">&quot;Array&quot;</code>; de ce fait, <span class="function"><a href="function.echo.php" class="function">echo</a></span> et
   <span class="function"><a href="function.print.php" class="function">print</a></span> ne peuvent pas à eux seuls afficher le contenu d&#039;un
   <span class="type">tableau</span>. Pour afficher un seul élément, utiliser une construction telle que
   <code class="literal">echo $arr[&#039;foo&#039;]</code>. Consulter ci-dessous les conseils sur la visualisation de tout le contenu.
  </p>

  <p class="para">
   Afin de convertir des <span class="type">objets</span> en <span class="type">chaînes</span>, la méthode magique
   <a href="language.oop5.magic.php" class="link">__toString</a> doit être utilisée.
  </p>

  <p class="para">
   Les <span class="type">ressources</span> sont toujours converties en <span class="type">chaînes</span> avec la
   structure <code class="literal">&quot;Resource id #1&quot;</code>, où <code class="literal">1</code>
   est le numéro de ressource attribué au <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> par PHP à
   l&#039;exécution. Bien que la structure exacte de cette chaîne ne doive pas être considérée comme
   fiable et soit sujette à changement, elle sera toujours unique pour une ressource donnée
   pendant la durée d&#039;exécution d&#039;un script (c&#039;est-à-dire une requête Web ou un processus CLI)
   et ne sera pas réutilisée. Pour obtenir le type d&#039;une <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>, utiliser
   la fonction <span class="function"><a href="function.get-resource-type.php" class="function">get_resource_type()</a></span>.
  </p>

  <p class="para">
   <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> est toujours converti en une chaîne vide.
  </p>

  <p class="para">
   Comme indiqué ci-dessus, convertir directement un <span class="type">tableau</span>,
   un <span class="type">objet</span> ou une <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> en <span class="type">chaîne</span> ne fournit
   pas d&#039;informations utiles sur la valeur au-delà de son type. Consulter les fonctions
   <span class="function"><a href="function.print-r.php" class="function">print_r()</a></span> et <span class="function"><a href="function.var-dump.php" class="function">var_dump()</a></span> pour
   des moyens plus efficaces d&#039;inspecter le contenu de ces types.
  </p>

  <p class="para">
   La plupart des valeurs PHP peuvent également être converties en <span class="type">chaînes</span> pour un stockage permanent.
   Cette méthode est appelée sérialisation et est effectuée par la fonction
   <span class="function"><a href="function.serialize.php" class="function">serialize()</a></span>.
  </p>

 </div>

 <div class="sect2" id="language.types.string.details">

  <h3 class="title">Détails du type chaîne</h3>

  <p class="para">
   La <span class="type">chaîne</span> en PHP est implémentée comme un tableau d&#039;octets et un
   entier indiquant la longueur du tampon. Elle n&#039;a aucune information sur la façon
   dont ces octets se traduisent en caractères, laissant cette tâche au programmeur.
   Il n&#039;y a pas de limitations sur les valeurs que la chaîne peut être composée ; en
   particulier, les octets de valeur <code class="literal">0</code> (« octets NUL ») sont autorisés
   partout dans la chaîne (cependant, quelques fonctions, dites dans ce manuel de ne
   pas être « sûres pour les binaires », peuvent transmettre les chaînes à des bibliothèques
   qui ignorent les données après un octet NUL.)
  </p>
  <p class="para">
   Cette nature du type chaîne explique pourquoi il n&#039;y a pas de type « octet » distinct
   en PHP – les chaînes prennent ce rôle. Les fonctions qui ne renvoient pas de données
   textuelles – par exemple, des données arbitraires lues à partir d&#039;une socket réseau –
   renverront tout de même des chaînes.
  </p>
  <p class="para">
   Étant donné que PHP ne dicte pas un encodage spécifique pour les chaînes, on pourrait
   se demander comment les littéraux de chaînes sont encodés. Par exemple, la chaîne
   <code class="literal">&quot;á&quot;</code> est-elle équivalente à <code class="literal">&quot;\xE1&quot;</code> (ISO-8859-1),
   <code class="literal">&quot;\xC3\xA1&quot;</code> (UTF-8, forme C),
   <code class="literal">&quot;\x61\xCC\x81&quot;</code> (UTF-8, forme D) ou toute autre représentation
   possible ? La réponse est que la chaîne sera encodée de la manière dont elle est
   encodée dans le fichier script. Ainsi, si le script est écrit en ISO-8859-1, la
   chaîne sera encodée en ISO-8859-1 et vice versa. Cependant, cela ne s&#039;applique pas
   si Zend Multibyte est activé ; dans ce cas, le script peut être écrit dans un
   encodage arbitraire (qui est explicitement déclaré ou détecté) et ensuite converti
   dans un certain encodage interne, qui sera ensuite l&#039;encodage utilisé pour les
   littéraux de chaînes.
   Il est à noter qu&#039;il y a certaines contraintes sur l&#039;encodage du script (ou sur l&#039;encodage
   interne, si Zend Multibyte est activé) – cela signifie presque toujours que cet
   encodage doit être un superset compatible de l&#039;ASCII, tel que UTF-8 ou ISO-8859-1.
   Il est cependant à noter que les encodages dépendants de l&#039;état où les mêmes valeurs d&#039;octets
   peuvent être utilisées dans des états de décalage initiaux et non initiaux peuvent
   poser problème.
  </p>
  <p class="para">
   Bien sûr, afin d&#039;être utiles, les fonctions qui opèrent sur du texte peuvent devoir
   faire certaines hypothèses sur la façon dont la chaîne est encodée. Malheureusement,
   il y a beaucoup de variations à ce sujet dans les fonctions de PHP :
  </p>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     Certaines fonctions supposent que la chaîne est encodée dans un (tout) encodage
     à un octet, mais elles n&#039;ont pas besoin d&#039;interpréter ces octets comme des
     caractères spécifiques. C&#039;est le cas, par exemple, de <span class="function"><a href="function.substr.php" class="function">substr()</a></span>,
     <span class="function"><a href="function.strpos.php" class="function">strpos()</a></span>, <span class="function"><a href="function.strlen.php" class="function">strlen()</a></span> ou
     <span class="function"><a href="function.strcmp.php" class="function">strcmp()</a></span>. Une autre façon de penser à ces fonctions est qu&#039;elles
     opèrent sur des tampons mémoire, c&#039;est-à-dire qu&#039;elles fonctionnent avec des octets
     et des décalages d&#039;octets.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     D&#039;autres fonctions reçoivent l&#039;encodage de la chaîne, supposant éventuellement un
     défaut si aucune information de ce type n&#039;est donnée. C&#039;est le cas de
     <span class="function"><a href="function.htmlentities.php" class="function">htmlentities()</a></span> et de la majorité des fonctions dans
     l&#039;<a href="book.mbstring.php" class="link">extension mbstring</a>.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     D&#039;autres utilisent la locale actuelle (voir <span class="function"><a href="function.setlocale.php" class="function">setlocale()</a></span>),
     mais fonctionnent octet par octet.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     Enfin, elles peuvent simplement supposer que la chaîne utilise un encodage
     spécifique, généralement UTF-8. C&#039;est le cas de la plupart des fonctions dans
     l&#039;<a href="book.intl.php" class="link">extension intl</a> et dans
     l&#039;<a href="book.pcre.php" class="link">extension PCRE</a>
     (dans ce dernier cas, seulement lorsque le modificateur <code class="literal">u</code> est
     utilisé).
    </span>
   </li>
  </ul>

  <p class="para">
   En fin de compte, cela signifie que l&#039;écriture de programmes corrects utilisant
   Unicode dépend d&#039;éviter soigneusement les fonctions qui ne fonctionneront pas et
   qui corrompront très probablement les données, et d&#039;utiliser à la place les
   fonctions qui se comportent correctement, généralement provenant des extensions
   <a href="book.intl.php" class="link">intl</a> et <a href="book.mbstring.php" class="link">mbstring</a>.
   Cependant, utiliser des fonctions capables de gérer les encodages Unicode n&#039;est que
   le début. Peu importe les fonctions que le langage fournit, il est essentiel de
   connaître la spécification Unicode. Par exemple, un programme qui suppose qu&#039;il
   n&#039;y a que des majuscules et des minuscules fait une hypothèse erronée.
  </p>
 </div>
</div><?php manual_footer($setup); ?>