<?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 => 'it',
  ),
  'this' => 
  array (
    0 => 'language.types.string.php',
    1 => 'Strings',
    2 => 'Strings',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Tipi di dati',
  ),
  'prev' => 
  array (
    0 => 'language.types.float.php',
    1 => 'Numeri floating point',
  ),
  'next' => 
  array (
    0 => 'language.types.numeric-strings.php',
    1 => 'Numeric strings',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    '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">Strings</h2>

 <p class="para">
  A <span class="type"><a href="language.types.string.php" class="type string">string</a></span> is a series of characters, where a character is
  the same as a byte. This means that PHP only supports a 256-character set,
  and hence does not offer native Unicode support. See
  <a href="language.types.string.php#language.types.string.details" class="link">details of the string
  type</a>.
 </p>

 <blockquote class="note"><p><strong class="note">Nota</strong>: 
  <span class="simpara">
   On 32-bit builds, a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> can be as large as up to 2GB
   (2147483647 bytes maximum)
  </span>
 </p></blockquote>

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

  <p class="para">
   A <span class="type"><a href="language.types.string.php" class="type string">string</a></span> literal can be specified in four different ways:
  </p>

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

  <div class="sect3" id="language.types.string.syntax.single">
   <h4 class="title">Single quoted</h4>

   <p class="para">
    The simplest way to specify a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> is to enclose it in single
    quotes (the character <code class="literal">&#039;</code>).
   </p>

   <p class="para">
    To specify a literal single quote, escape it with a backslash
    (<code class="literal">\</code>). To specify a literal backslash, double it
    (<code class="literal">\\</code>). All other instances of backslash will be treated
    as a literal backslash: this means that the other escape sequences you
    might be used to, such as <code class="literal">\r</code> or <code class="literal">\n</code>,
    will be output literally as specified rather than having any special
    meaning.
   </p>

   <blockquote class="note"><p><strong class="note">Nota</strong>: 
    <span class="simpara">
     Unlike the <a href="language.types.string.php#language.types.string.syntax.double" class="link">double-quoted</a>
     and <a href="language.types.string.php#language.types.string.syntax.heredoc" class="link">heredoc</a> syntaxes,
     <a href="language.variables.php" class="link">variables</a> and escape sequences
     for special characters will <em>not</em> be expanded when they
     occur in single quoted <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s.
    </span>
   </p></blockquote>

   <div class="example" id="example-1">
    <p><strong>Example #1 Syntax Variants</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'this is a simple string'</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">'You can also have embedded newlines in<br />strings this way as it is<br />okay to do'</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Outputs: Arnold once said: "I'll be back"<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Arnold once said: "I\'ll be back"'</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Outputs: You deleted C:\*.*?<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'You deleted C:\\*.*?'</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Outputs: You deleted C:\*.*?<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'You deleted C:\*.*?'</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Outputs: This will not expand: \n a newline<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'This will not expand: \n a newline'</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Outputs: Variables do not $expand $either<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Variables do not $expand $either'</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">Double quoted</h4>

   <p class="para">
    If the <span class="type"><a href="language.types.string.php" class="type string">string</a></span> is enclosed in double-quotes (<code class="literal">&quot;</code>), PHP will
    interpret the following escape sequences for special characters:
   </p>

   <table class="doctable table">
    <caption><strong>Escaped characters</strong></caption>

    
     <thead>
      <tr>
       <th>Sequence</th>
       <th>Meaning</th>
      </tr>

     </thead>


     <tbody class="tbody">
      <tr>
       <td><code class="literal">\n</code></td>
       <td>linefeed (LF or 0x0A (10) in ASCII)</td>
      </tr>

      <tr>
       <td><code class="literal">\r</code></td>
       <td>carriage return (CR or 0x0D (13) in ASCII)</td>
      </tr>

      <tr>
       <td><code class="literal">\t</code></td>
       <td>horizontal tab (HT or 0x09 (9) in ASCII)</td>
      </tr>

      <tr>
       <td><code class="literal">\v</code></td>
       <td>vertical tab (VT or 0x0B (11) in ASCII)</td>
      </tr>

      <tr>
       <td><code class="literal">\e</code></td>
       <td>escape (ESC or 0x1B (27) in ASCII)</td>
      </tr>

      <tr>
       <td><code class="literal">\f</code></td>
       <td>form feed (FF or 0x0C (12) in ASCII)</td>
      </tr>

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

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

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

      <tr>
       <td><code class="literal">\[0-7]{1,3}</code></td>
       <td>
        Octal: the sequence of characters matching the regular expression <code class="literal">[0-7]{1,3}</code>
        is a character in octal notation (e.g. <code class="literal">&quot;\101&quot; === &quot;A&quot;</code>),
        which silently overflows to fit in a byte (e.g. <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>
        Hexadecimal: the sequence of characters matching the regular expression
        <code class="literal">[0-9A-Fa-f]{1,2}</code> is a character in hexadecimal notation
        (e.g. <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: the sequence of characters matching the regular expression <code class="literal">[0-9A-Fa-f]+</code>
        is a Unicode codepoint, which will be output to the string as that codepoint&#039;s UTF-8 representation.
        The braces are required in the sequence. E.g. <code class="literal">&quot;\u{41}&quot; === &quot;A&quot;</code>
       </td>
      </tr>

     </tbody>
    
   </table>


   <p class="para">
    As in single quoted <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s, escaping any other character will
    result in the backslash being printed too.
   </p>

   <p class="para">
    The most important feature of double-quoted <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s is the fact
    that variable names will be expanded. See
    <a href="language.types.string.php#language.types.string.parsing" class="link">string interpolation</a> for
    details.
   </p>
  </div>

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

   <p class="simpara">
    A third way to delimit <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s is the heredoc syntax:
    <code class="literal">&lt;&lt;&lt;</code>. After this operator, an identifier is
    provided, then a newline. The <span class="type"><a href="language.types.string.php" class="type string">string</a></span> itself follows, and then
    the same identifier again to close the quotation.
   </p>

   <p class="simpara">
    The closing identifier may be indented by space or tab, in which case
    the indentation will be stripped from all lines in the doc string.
    Prior to PHP 7.3.0, the closing identifier <em>must</em>
    begin in the first column of the line.
   </p>

   <p class="simpara">
    Also, the closing identifier must follow the same naming rules as any
    other label in PHP: it must contain only alphanumeric characters and
    underscores, and must start with a non-digit character or underscore.
   </p>

   <div class="example" id="example-2">
    <p><strong>Example #2 Basic Heredoc example as of PHP 7.3.0</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// no 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 spaces of 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>Output of the above example in PHP 7.3:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
      a
     b
    c

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

   <p class="simpara">
    If the closing identifier is indented further than any lines of the body, then a <span class="classname"><a href="class.parseerror.php" class="classname">ParseError</a></span> will be thrown:
   </p>

   <div class="example" id="example-3">
    <p><strong>Example #3 Closing identifier must not be indented further than any lines of the body</strong></p>
    <div class="example-contents">
<div class="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>Output of the above example in PHP 7.3:</p></div>
    <div class="example-contents screen">
<div class="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">
    If the closing identifier is indented, tabs can be used as well, however,
    tabs and spaces <em>must not</em> be intermixed regarding
    the indentation of the closing identifier and the indentation of the body
     (up to the closing identifier). In any of these cases, a <span class="classname"><a href="class.parseerror.php" class="classname">ParseError</a></span> will be thrown.

    These whitespace constraints have been included because mixing tabs and
    spaces for indentation is harmful to legibility.
   </p>

   <div class="example" id="example-4">
    <p><strong>Example #4 Different indentation for body (spaces) closing identifier</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">// All the following code do not work.<br /><br />// different indentation for body (spaces) ending marker (tabs)<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">// mixing spaces and tabs in body<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">// mixing spaces and tabs in ending marker<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>Output of the above example in PHP 7.3:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Parse error: Invalid indentation - tabs and spaces cannot be mixed in example.php line 8
</pre></div>
    </div>
   </div>

   <p class="simpara">
    The closing identifier for the body string is not required to be
    followed by a semicolon or newline. For example, the following code
    is allowed as of PHP 7.3.0:
   </p>

   <div class="example" id="example-5">
    <p><strong>Example #5 Continuing an expression after a closing identifier</strong></p>
    <div class="example-contents">
<div class="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>Output of the above example in PHP 7.3:</p></div>
    <div class="example-contents screen">
<div class="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">Avviso</strong>
    <p class="simpara">
     If the closing identifier was found at the start of a line, then
     regardless of whether it was a part of another word, it may be considered
     as the closing identifier and causes a <span class="classname"><a href="class.parseerror.php" class="classname">ParseError</a></span>.
    </p>

    <div class="example" id="example-6">
     <p><strong>Example #6 Closing identifier in body of the string tends to cause ParseError</strong></p>
     <div class="example-contents">
<div class="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>Output of the above example in PHP 7.3:</p></div>
    <div class="example-contents screen">
<div class="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">
     To avoid this problem, it is safe to follow the simple rule:
     <em>do not choose a word that appears in the body of the text
     as a closing identifier</em>.
    </p>

   </div>

   <div class="warning"><strong class="warning">Avviso</strong>
    <p class="simpara">
     Prior to PHP 7.3.0, it is very important to note that the line with the
     closing identifier must contain no other characters, except a semicolon
     (<code class="literal">;</code>).
     That means especially that the identifier
     <em>may not be indented</em>, and there may not be any spaces
     or tabs before or after the semicolon. It&#039;s also important to realize that
     the first character before the closing identifier must be a newline as
     defined by the local operating system. This is <code class="literal">\n</code> on
     UNIX systems, including macOS. The closing delimiter must also be
     followed by a newline.
    </p>

    <p class="simpara">
     If this rule is broken and the closing identifier is not &quot;clean&quot;, it will
     not be considered a closing identifier, and PHP will continue looking for
     one. If a proper closing identifier is not found before the end of the
     current file, a parse error will result at the last line.
    </p>

    <div class="example" id="example-7">
     <p><strong>Example #7 Invalid example, prior to PHP 7.3.0</strong></p>
     <div class="example-contents">
      
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </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">// Identifier must not be indented<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
    <div class="example" id="example-8">
     <p><strong>Example #8 Valid example, even prior to PHP 7.3.0</strong></p>
     <div class="example-contents">
      
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </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">
     Heredocs containing variables can not be used for initializing class properties.
    </p>

   </div>

   <p class="para">
    Heredoc text behaves just like a double-quoted <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, without
    the double quotes. This means that quotes in a heredoc do not need to be
    escaped, but the escape codes listed above can still be used. Variables are
    expanded, but the same care must be taken when expressing complex variables
    inside a heredoc as with <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>Example #9 Heredoc string quoting example</strong></p>
    <div class="example-contents">
<div class="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">Example of string<br />spanning multiple lines<br />using heredoc syntax.<br /></span><span style="color: #007700">EOD;<br /><br /></span><span style="color: #FF8000">/* More complex example, with 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">My name is "</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">". I am printing some </span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #DD0000">.<br />Now, I am printing some </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 />This should print a capital 'A': \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>Il precedente esempio visualizzerà:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
My name is &quot;MyName&quot;. I am printing some Foo.
Now, I am printing some Bar2.
This should print a capital &#039;A&#039;: A</pre></div>
    </div>
   </div>

   <p class="para">
    It is also possible to use the Heredoc syntax to pass data to function
    arguments:
   </p>

   <div class="example" id="example-10">
    <p><strong>Example #10 Heredoc in arguments example</strong></p>
    <div class="example-contents">
<div class="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">
    It&#039;s possible to initialize static variables and class
    properties/constants using the Heredoc syntax:
   </p>

   <div class="example" id="example-11">
    <p><strong>Example #11 Using Heredoc to initialize static values</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">// Static variables<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">Nothing in here...<br /></span><span style="color: #007700">LABEL;<br />}<br /><br /></span><span style="color: #FF8000">// Class properties/constants<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">Constant example<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">Property example<br /></span><span style="color: #007700">FOOBAR;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    The opening Heredoc identifier may optionally be
    enclosed in double quotes:
   </p>

   <div class="example" id="example-12">
    <p><strong>Example #12 Using double quotes in Heredoc</strong></p>
    <div class="example-contents">
<div class="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">Hello World!<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">
    Nowdocs are to single-quoted strings what heredocs are to double-quoted
    strings. A nowdoc is specified similarly to a heredoc, but <em>no
    String interpolation is done</em> inside a nowdoc. The construct is ideal for
    embedding PHP code or other large blocks of text without the need for
    escaping. It shares some features in common with the SGML
    <code class="literal">&lt;![CDATA[ ]]&gt;</code> construct, in that it declares a
    block of text which is not for parsing.
   </p>

   <p class="para">
    A nowdoc is identified with the same <code class="literal">&lt;&lt;&lt;</code>
    sequence used for heredocs, but the identifier which follows is enclosed in
    single quotes, e.g. <code class="literal">&lt;&lt;&lt;&#039;EOT&#039;</code>. All the rules for
    heredoc identifiers also apply to nowdoc identifiers, especially those
    regarding the appearance of the closing identifier.
   </p>

   <div class="example" id="example-13">
    <p><strong>Example #13 Nowdoc string quoting example</strong></p>
    <div class="example-contents">
<div class="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">Example of string spanning multiple lines<br />using nowdoc syntax. Backslashes are always treated literally,<br />e.g. \\ and \'.<br /></span><span style="color: #007700">EOD;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Il precedente esempio visualizzerà:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Example of string spanning multiple lines
using nowdoc syntax. Backslashes are always treated literally,
e.g. \\ and \&#039;.
</pre></div>
    </div>
   </div>

   <div class="example" id="example-14">
    <p><strong>Example #14 Nowdoc string quoting example with variables</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </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">My name is "$name". I am printing some $foo-&gt;foo.<br />Now, I am printing some {$foo-&gt;bar[1]}.<br />This should not print a capital 'A': \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>Il precedente esempio visualizzerà:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
My name is &quot;$name&quot;. I am printing some $foo-&gt;foo.
Now, I am printing some {$foo-&gt;bar[1]}.
This should not print a capital &#039;A&#039;: \x41</pre></div>
    </div>
   </div>

   <div class="example" id="example-15">
    <p><strong>Example #15 Static data example</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">String interpolation</h4>

   <p class="simpara">
    When a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> is specified in double quotes or with heredoc,
    <a href="language.variables.php" class="link">variables</a> can be substituted within it.
   </p>

   <p class="simpara">
    There are two types of syntax: a
    <a href="language.types.string.php#language.types.string.parsing.basic" class="link">basic</a> one and an
    <a href="language.types.string.php#language.types.string.parsing.advanced" class="link">advanced</a> one.
    The basic syntax is the most common and convenient. It provides a way to
    embed a variable, an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> value, or an <span class="type"><a href="language.types.object.php" class="type object">object</a></span>
    property in a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> with a minimum of effort.
   </p>

   <div class="sect4" id="language.types.string.parsing.basic">
    <h5 class="title">Basic syntax</h5>
    <p class="simpara">
     If a dollar sign (<code class="literal">$</code>) is encountered, the characters
     that follow it which can be used in a variable name will be interpreted
     as such and substituted.
    </p>
    <div class="example" id="example-16">
     <p><strong>Example #16 String Interpolation</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$juice </span><span style="color: #007700">= </span><span style="color: #DD0000">"apple"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"He drank some </span><span style="color: #0000BB">$juice</span><span style="color: #DD0000"> juice." </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>Il precedente esempio visualizzerà:</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
He drank some apple juice.
</pre></div>
     </div>
    </div>

    <p class="simpara">
     Formally, the structure for the basic variable substitution syntax is
     as follows:
    </p>
    <div class="informalexample">
     <div class="example-contents">
<div class="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">Avviso</strong>
     <p class="para">
      The <code class="literal">${ expression }</code> syntax is deprecated as of
      PHP 8.2.0, as it can be interpreted as
      <a href="language.variables.variable.php" class="link">variable variables</a>:
      <div class="informalexample">
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">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">Output of the above example in PHP 8.2:</p>
       <div class="example-contents screen">
<div class="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">Il precedente esempio visualizzerà:</p>
       <div class="example-contents screen">
<div class="cdata"><pre>
string(3) &quot;foo&quot;
string(3) &quot;bar&quot;
</pre></div>
       </div>
      </div>
      The <a href="language.types.string.php#language.types.string.parsing.advanced" class="link">advanced</a>
      string interpolation syntax should be used instead.
     </p>
    </div>

    <blockquote class="note"><p><strong class="note">Nota</strong>: 
     <span class="simpara">
      If it is not possible to form a valid name the dollar sign remains
      as verbatim in the string:
     </span>
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"No interpolation $  has happened\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"No interpolation $\n has happened\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"No interpolation $2 has happened\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

      <p class="para">Il precedente esempio visualizzerà:</p>
      <div class="example-contents screen">
<div class="cdata"><pre>
No interpolation $  has happened
No interpolation $
 has happened
No interpolation $2 has happened
</pre></div>
      </div>
     </div>
    </p></blockquote>

    <div class="example" id="example-17">
     <p><strong>Example #17 Interpolating the value of the first dimension of an array or property</strong></p>
     <div class="example-contents">
<div class="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">"apple"</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">"purple"</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"He drank some </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"> juice."</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">"He drank some </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"> juice."</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">"He drank some </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"> juice."</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">"string"</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">"Object value: </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>Il precedente esempio visualizzerà:</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
He drank some apple juice.
He drank some orange juice.
He drank some purple juice.
Object value: string.
</pre></div>
     </div>
    </div>

    <blockquote class="note"><p><strong class="note">Nota</strong>: 
     <span class="simpara">
      The array key must be unquoted, and it is therefore not possible to
      refer to a constant as a key with the basic syntax. Use the
      <a href="language.types.string.php#language.types.string.parsing.advanced" class="link">advanced</a>
      syntax instead.
     </span>
    </p></blockquote>

    <p class="simpara">
     As of PHP 7.1.0 also <em>negative</em> numeric indices are
     supported.
    </p>

    <div class="example" id="example-18"><p><strong>Example #18 Negative numeric indices</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$string </span><span style="color: #007700">= </span><span style="color: #DD0000">'string'</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"The character at index -2 is </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">"Changing the character at index -3 to o gives </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>Il precedente esempio visualizzerà:</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
The character at index -2 is n.
Changing the character at index -3 to o gives strong.
</pre></div>
     </div>
    </div>

    <p class="simpara">
     For anything more complex, the
     <a href="language.types.string.php#language.types.string.parsing.advanced" class="link">advanced</a>
     syntax must be used.
    </p>
   </div>

   <div class="sect4" id="language.types.string.parsing.advanced">
    <h5 class="title">Advanced (curly) syntax</h5>

    <p class="simpara">
     The advanced syntax permits the interpolation of
     <em>variables</em> with arbitrary accessors.
    </p>

    <p class="simpara">
     Any scalar variable, array element or object property
     (<span class="modifier">static</span> or not) with a
     <span class="type"><a href="language.types.string.php" class="type string">string</a></span> representation can be included via this syntax.
     The expression is written the same way as it would appear outside the
     <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, and then wrapped in <code class="literal">{</code> and
     <code class="literal">}</code>. Since <code class="literal">{</code> can not be escaped, this
     syntax will only be recognised when the <code class="literal">$</code> immediately
     follows the <code class="literal">{</code>. Use <code class="literal">{\$</code> to get a
     literal <code class="literal">{$</code>. Some examples to make it clear:
    </p>

    <div class="example" id="example-19">
     <p><strong>Example #19 Curly Syntax</strong></p>
     <div class="example-contents">
<div class="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">'fantastic'</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">'Indexed value'</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'const-key' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Key with minus sign'</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">// Won't work, outputs: This is { fantastic}<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This is { </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">// Works, outputs: This is fantastic<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This is </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">// To show curly braces in the output:<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This is {</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">// Works<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This square is </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 centimeters wide." </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /><br /></span><span style="color: #FF8000">// Works, quoted keys only work using the curly brace syntax<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This works: </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 /><br /></span><span style="color: #FF8000">// Works<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This works: </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">"This works: </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">// When using multidimensional arrays, always use braces around arrays<br />// when inside of strings<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This works: </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">"This works: </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">"This works: </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">// Won't work, outputs: 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">// Works, outputs: 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">Nota</strong>: 
     <span class="simpara">
      As this syntax allows arbitrary expressions it is possible to use
      <a href="language.variables.variable.php" class="link">variable variables</a>
      within the advanced syntax.
     </span>
    </p></blockquote>
   </div>
  </div>

  <div class="sect3" id="language.types.string.substr">
   <h4 class="title">String access and modification by character</h4>

   <p class="para">
    Characters within <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s may be accessed and modified by
    specifying the zero-based offset of the desired character after the
    <span class="type"><a href="language.types.string.php" class="type string">string</a></span> using square <span class="type"><a href="language.types.array.php" class="type array">array</a></span> brackets, as in
    <var class="varname">$str[42]</var>. Think of a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> as an
    <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of characters for this purpose. The functions
    <span class="function"><a href="function.substr.php" class="function">substr()</a></span> and <span class="function"><a href="function.substr-replace.php" class="function">substr_replace()</a></span>
    can be used when you want to extract or replace more than 1 character.
   </p>

   <blockquote class="note"><p><strong class="note">Nota</strong>: 
    <span class="simpara">
     As of PHP 7.1.0, negative string offsets are also supported. These specify
     the offset from the end of the string.
     Formerly, negative offsets emitted <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> for reading
     (yielding an empty string) and <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> for writing
     (leaving the string untouched).
    </span>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Nota</strong>: 
    <span class="simpara">
     Prior to PHP 8.0.0, <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s could also be accessed using braces, as in
     <var class="varname">$str{42}</var>, for the same purpose.
     This curly brace syntax was deprecated as of PHP 7.4.0 and no longer supported as of PHP 8.0.0.
    </span>
   </p></blockquote>

   <div class="warning"><strong class="warning">Avviso</strong>
    <p class="simpara">
     Writing to an out of range offset pads the string with spaces.
     Non-integer types are converted to integer.
     Illegal offset type emits <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
     Only the first character of an assigned string is used.
     As of PHP 7.1.0, assigning an empty string throws a fatal error. Formerly,
     it assigned a NULL byte.
    </p>
   </div>

   <div class="warning"><strong class="warning">Avviso</strong>
    <p class="simpara">
     Internally, PHP strings are byte arrays. As a result, accessing or
     modifying a string using array brackets is not multi-byte safe, and
     should only be done with strings that are in a single-byte encoding such
     as ISO-8859-1.
    </p>
   </div>

   <blockquote class="note"><p><strong class="note">Nota</strong>: 
    <span class="simpara">
     As of PHP 7.1.0, applying the empty index operator on an empty string throws a fatal
     error. Formerly, the empty string was silently converted to an array.
    </span>
   </p></blockquote>

   <div class="example" id="example-20">
    <p><strong>Example #20 Some string examples</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Get the first character of a string<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'This is a 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">// Get the third character of a string<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">// Get the last character of a string.<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'This is still a 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">// Modify the last character of a string<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'Look at the sea'</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">
    String offsets have to either be integers or integer-like strings,
    otherwise a warning will be thrown.
   </p>

   <div class="example" id="example-21">
    <p><strong>Example #21 Example of Illegal String Offsets</strong></p>
    <div class="example-contents">
<div class="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>Il precedente esempio visualizzerà:</p></div>
    <div class="example-contents screen">
<div class="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">Nota</strong>: 
    <p class="para">
     Accessing variables of other types (not including arrays or objects
     implementing the appropriate interfaces) using <code class="literal">[]</code> or
     <code class="literal">{}</code> silently returns <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
    </p>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Nota</strong>: 
    <p class="para">
     Characters within string literals can be accessed
     using <code class="literal">[]</code> or <code class="literal">{}</code>.
    </p>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Nota</strong>: 
    <p class="para">
     Accessing characters within string literals using the
     <code class="literal">{}</code> syntax has been deprecated in PHP 7.4.
     This has been removed in PHP 8.0.
    </p>
   </p></blockquote>
  </div>
 </div>

 <div class="sect2" id="language.types.string.useful-funcs">
  <h3 class="title">Useful functions and operators</h3>

  <p class="para">
   <span class="type"><a href="language.types.string.php" class="type String">String</a></span>s may be concatenated using the &#039;.&#039; (dot) operator. Note
   that the &#039;+&#039; (addition) operator will <em>not</em> work for this.
   See <a href="language.operators.string.php" class="link">String operators</a> for
   more information.
  </p>

  <p class="para">
   There are a number of useful functions for <span class="type"><a href="language.types.string.php" class="type string">string</a></span> manipulation.
  </p>

  <p class="simpara">
   See the <a href="ref.strings.php" class="link">string functions section</a> for
   general functions, and the <a href="ref.pcre.php" class="link">Perl-compatible regular
   expression functions</a> for advanced find &amp; replace functionality.
  </p>

  <p class="simpara">
   There are also <a href="ref.url.php" class="link">functions for URL strings</a>, and
   functions to encrypt/decrypt strings
   (<a href="ref.sodium.php" class="link">Sodium</a> and
   <a href="ref.hash.php" class="link">Hash</a>).
  </p>

  <p class="simpara">
   Finally, see also the <a href="ref.ctype.php" class="link">character type
   functions</a>.
  </p>
 </div>

 <div class="sect2" id="language.types.string.casting">
  <h3 class="title">Converting to string</h3>

  <p class="para">
   A value can be converted to a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> using the
   <code class="literal">(string)</code> cast or the <span class="function"><a href="function.strval.php" class="function">strval()</a></span> function.
   <span class="type"><a href="language.types.string.php" class="type String">String</a></span> conversion is automatically done in the scope of an
   expression where a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> is needed. This happens when using the
   <span class="function"><a href="function.echo.php" class="function">echo</a></span> or <span class="function"><a href="function.print.php" class="function">print</a></span> functions, or when a
   variable is compared to a <span class="type"><a href="language.types.string.php" class="type string">string</a></span>. The sections on
   <a href="language.types.php" class="link">Types</a> and
   <a href="language.types.type-juggling.php" class="link">Type Juggling</a> will make
   the following clearer. See also the <span class="function"><a href="function.settype.php" class="function">settype()</a></span> function.
  </p>

  <p class="para">
   A <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> value is converted to the <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
   <code class="literal">&quot;1&quot;</code>. <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> is converted to
   <code class="literal">&quot;&quot;</code> (the empty string). This allows conversion back and
   forth between <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> and <span class="type"><a href="language.types.string.php" class="type string">string</a></span> values.
  </p>

  <p class="para">
   An <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> or <span class="type"><a href="language.types.float.php" class="type float">float</a></span> is converted to a
   <span class="type"><a href="language.types.string.php" class="type string">string</a></span> representing the number textually (including the
   exponent part for <span class="type"><a href="language.types.float.php" class="type float">float</a></span>s). Floating point numbers can be
   converted using exponential notation (<code class="literal">4.1E+6</code>).
  </p>

  <blockquote class="note"><p><strong class="note">Nota</strong>: 
   <p class="para">
    As of PHP 8.0.0, the decimal point character is always
    a period (&quot;<code class="literal">.</code>&quot;). Prior to PHP 8.0.0,
    the decimal point character is defined in the script&#039;s locale (category
    LC_NUMERIC). See the <span class="function"><a href="function.setlocale.php" class="function">setlocale()</a></span> function.
   </p>
  </p></blockquote>

  <p class="para">
   <span class="type"><a href="language.types.array.php" class="type Array">Array</a></span>s are always converted to the <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
   <code class="literal">&quot;Array&quot;</code>; because of this, <span class="function"><a href="function.echo.php" class="function">echo</a></span> and
   <span class="function"><a href="function.print.php" class="function">print</a></span> can not by themselves show the contents of an
   <span class="type"><a href="language.types.array.php" class="type array">array</a></span>. To view a single element, use a construction such as
   <code class="literal">echo $arr[&#039;foo&#039;]</code>. See below for tips on viewing the entire
   contents.
  </p>

  <p class="para">
   In order to convert <span class="type"><a href="language.types.object.php" class="type object">object</a></span>s to <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, the magic
   method <a href="language.oop5.magic.php" class="link">__toString</a> must be used.
  </p>

  <p class="para">
   <span class="type"><a href="language.types.resource.php" class="type Resource">Resource</a></span>s are always converted to <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s with the
   structure <code class="literal">&quot;Resource id #1&quot;</code>, where <code class="literal">1</code>
   is the resource number assigned to the <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> by PHP at
   runtime. While the exact structure of this string should not be relied on
   and is subject to change, it will always be unique for a given resource
   within the lifetime of a script being executed (ie a Web request or CLI
   process) and won&#039;t be reused. To get a <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>&#039;s type, use
   the <span class="function"><a href="function.get-resource-type.php" class="function">get_resource_type()</a></span> function.
  </p>

  <p class="para">
   <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> is always converted to an empty string.
  </p>

  <p class="para">
   As stated above, directly converting an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>,
   <span class="type"><a href="language.types.object.php" class="type object">object</a></span>, or <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> to a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> does
   not provide any useful information about the value beyond its type. See the
   functions <span class="function"><a href="function.print-r.php" class="function">print_r()</a></span> and <span class="function"><a href="function.var-dump.php" class="function">var_dump()</a></span> for
   more effective means of inspecting the contents of these types.
  </p>

  <p class="para">
   Most PHP values can also be converted to <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s for permanent
   storage. This method is called serialization, and is performed by the
   <span class="function"><a href="function.serialize.php" class="function">serialize()</a></span> function.
  </p>

 </div>

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

  <h3 class="title">Details of the String Type</h3>

  <p class="para">
   The <span class="type"><a href="language.types.string.php" class="type string">string</a></span> in PHP is implemented as an array of bytes and an
   integer indicating the length of the buffer. It has no information about how
   those bytes translate to characters, leaving that task to the programmer.
   There are no limitations on the values the string can be composed of; in
   particular, bytes with value <code class="literal">0</code> (“NUL bytes”) are allowed
   anywhere in the string (however, a few functions, said in this manual not to
   be “binary safe”, may hand off the strings to libraries that ignore data
   after a NUL byte.)
  </p>
  <p class="para">
   This nature of the string type explains why there is no separate “byte” type
   in PHP – strings take this role. Functions that return no textual data – for
   instance, arbitrary data read from a network socket – will still return
   strings.
  </p>
  <p class="para">
   Given that PHP does not dictate a specific encoding for strings, one might
   wonder how string literals are encoded. For instance, is the string
   <code class="literal">&quot;á&quot;</code> equivalent to <code class="literal">&quot;\xE1&quot;</code> (ISO-8859-1),
   <code class="literal">&quot;\xC3\xA1&quot;</code> (UTF-8, C form),
   <code class="literal">&quot;\x61\xCC\x81&quot;</code> (UTF-8, D form) or any other possible
   representation? The answer is that string will be encoded in whatever fashion
   it is encoded in the script file. Thus, if the script is written in
   ISO-8859-1, the string will be encoded in ISO-8859-1 and so on. However,
   this does not apply if Zend Multibyte is enabled; in that case, the script
   may be written in an arbitrary encoding (which is explicitly declared or is
   detected) and then converted to a certain internal encoding, which is then
   the encoding that will be used for the string literals.
   Note that there are some constraints on the encoding of the script (or on the
   internal encoding, should Zend Multibyte be enabled) – this almost always
   means that this encoding should be a compatible superset of ASCII, such as
   UTF-8 or ISO-8859-1. Note, however, that state-dependent encodings where
   the same byte values can be used in initial and non-initial shift states
   may be problematic.
  </p>
  <p class="para">
   Of course, in order to be useful, functions that operate on text may have to
   make some assumptions about how the string is encoded. Unfortunately, there
   is much variation on this matter throughout PHP’s functions:
  </p>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     Some functions assume that the string is encoded in some (any) single-byte
     encoding, but they do not need to interpret those bytes as specific
     characters. This is case of, for instance, <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> or
     <span class="function"><a href="function.strcmp.php" class="function">strcmp()</a></span>. Another way to think of these functions is
     that operate on memory buffers, i.e., they work with bytes and byte
     offsets.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     Other functions are passed the encoding of the string, possibly they also
     assume a default if no such information is given. This is the case of
     <span class="function"><a href="function.htmlentities.php" class="function">htmlentities()</a></span> and the majority of the
     functions in the <a href="book.mbstring.php" class="link">mbstring</a> extension.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     Others use the current locale (see <span class="function"><a href="function.setlocale.php" class="function">setlocale()</a></span>), but
     operate byte-by-byte.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     Finally, they may just assume the string is using a specific encoding,
     usually UTF-8. This is the case of most functions in the
     <a href="book.intl.php" class="link">intl</a> extension and in the
     <a href="book.pcre.php" class="link">PCRE</a> extension
     (in the last case, only when the <code class="literal">u</code> modifier is used).
    </span>
   </li>
  </ul>

  <p class="para">
   Ultimately, this means writing correct programs using Unicode depends on
   carefully avoiding functions that will not work and that most likely will
   corrupt the data and using instead the functions that do behave correctly,
   generally from the <a href="book.intl.php" class="link">intl</a> and
   <a href="book.mbstring.php" class="link">mbstring</a> extensions.
   However, using functions that can handle Unicode encodings is just the
   beginning. No matter the functions the language provides, it is essential to
   know the Unicode specification. For instance, a program that assumes there is
   only uppercase and lowercase is making a wrong assumption.
  </p>
 </div>
</div><?php manual_footer($setup); ?>