Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8968

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Strings</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.types.float.html">Floating point numbers</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.types.array.html">Arrays</a></div>
 <div class="up"><a href="language.types.html">Types</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.types.string" class="sect1">
 <h2 class="title">Strings</h2>

 
 <p class="para">
  A <span class="type"><a href="language.types.string.html" class="type string">string</a></span> is 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.html#language.types.string.details" class="link">details of the string
  type</a>.
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <span class="simpara">
   <span class="type"><a href="language.types.string.html" 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.html" 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.html#language.types.string.syntax.single" class="link">single quoted</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.string.html#language.types.string.syntax.double" class="link">double quoted</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.string.html#language.types.string.syntax.heredoc" class="link">heredoc syntax</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.string.html#language.types.string.syntax.nowdoc" class="link">nowdoc syntax</a>
     (since PHP 5.3.0)
    </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.html" class="type string">string</a></span> is to enclose it in single
    quotes (the character <em>&#039;</em>).
   </p>

   <p class="para">
    To specify a literal single quote, escape it with a backslash
    (<em>\</em>). To specify a literal backslash, double it
    (<em>\\</em>). 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 <em>\r</em> or <em>\n</em>,
    will be output literally as specified rather than having any special
    meaning.
   </p>

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

   <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&nbsp;</span><span style="color: #DD0000">'this&nbsp;is&nbsp;a&nbsp;simple&nbsp;string'</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">'You&nbsp;can&nbsp;also&nbsp;have&nbsp;embedded&nbsp;newlines&nbsp;in&nbsp;<br />strings&nbsp;this&nbsp;way&nbsp;as&nbsp;it&nbsp;is<br />okay&nbsp;to&nbsp;do'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Outputs:&nbsp;Arnold&nbsp;once&nbsp;said:&nbsp;"I'll&nbsp;be&nbsp;back"<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'Arnold&nbsp;once&nbsp;said:&nbsp;"I\'ll&nbsp;be&nbsp;back"'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Outputs:&nbsp;You&nbsp;deleted&nbsp;C:\*.*?<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'You&nbsp;deleted&nbsp;C:\\*.*?'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Outputs:&nbsp;You&nbsp;deleted&nbsp;C:\*.*?<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'You&nbsp;deleted&nbsp;C:\*.*?'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Outputs:&nbsp;This&nbsp;will&nbsp;not&nbsp;expand:&nbsp;\n&nbsp;a&nbsp;newline<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'This&nbsp;will&nbsp;not&nbsp;expand:&nbsp;\n&nbsp;a&nbsp;newline'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Outputs:&nbsp;Variables&nbsp;do&nbsp;not&nbsp;$expand&nbsp;$either<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'Variables&nbsp;do&nbsp;not&nbsp;$expand&nbsp;$either'</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.html" class="type string">string</a></span> is enclosed in double-quotes (&quot;), PHP will
    interpret more 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><em>\n</em></td>
       <td>linefeed (LF or 0x0A (10) in ASCII)</td>
      </tr>

      <tr>
       <td><em>\r</em></td>
       <td>carriage return (CR or 0x0D (13) in ASCII)</td>
      </tr>

      <tr>
       <td><em>\t</em></td>
       <td>horizontal tab (HT or 0x09 (9) in ASCII)</td>
      </tr>

      <tr>
       <td><em>\v</em></td>
       <td>vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)</td>
      </tr>

      <tr>
       <td><em>\e</em></td>
       <td>escape (ESC or 0x1B (27) in ASCII) (since PHP 5.4.0)</td>
      </tr>

      <tr>
       <td><em>\f</em></td>
       <td>form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)</td>
      </tr>

      <tr>
       <td><em>\\</em></td>
       <td>backslash</td>
      </tr>

      <tr>
       <td><em>\$</em></td>
       <td>dollar sign</td>
      </tr>

      <tr>
       <td><em>\&quot;</em></td>
       <td>double-quote</td>
      </tr>

      <tr>
       <td><em>\[0-7]{1,3}</em></td>
       <td>
        the sequence of characters matching the regular expression is a
        character in octal notation
       </td>
      </tr>

      <tr>
       <td><em>\x[0-9A-Fa-f]{1,2}</em></td>
       <td>
        the sequence of characters matching the regular expression is a
        character in hexadecimal notation
       </td>
      </tr>

     </tbody>
    
   </table>


   <p class="para">
    As in single quoted <span class="type"><a href="language.types.string.html" class="type string">string</a></span>s, escaping any other character will
    result in the backslash being printed too. Before PHP 5.1.1, the backslash
    in <em>\{$var}</em> had not been printed.
   </p>

   <p class="para">
    The most important feature of double-quoted <span class="type"><a href="language.types.string.html" class="type string">string</a></span>s is the fact
    that variable names will be expanded. See
    <a href="language.types.string.html#language.types.string.parsing" class="link">string parsing</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.html" class="type string">string</a></span>s is the heredoc syntax:
    <em>&lt;&lt;&lt;</em>. After this operator, an identifier is
    provided, then a newline. The <span class="type"><a href="language.types.string.html" 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 <em class="emphasis">must</em> begin in the first column
    of the line. Also, the 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="warning"><strong class="warning">Warning</strong>
    <p class="simpara">
     It is very important to note that the line with the closing identifier must
     contain no other characters, except a semicolon (<em>;</em>). 
     That means especially that the identifier
     <em class="emphasis">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 <em>\n</em> on
     UNIX systems, including Mac OS X. 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>

    <p class="para">
     Heredocs can not be used for initializing class properties. Since PHP 5.3,
     this limitation is valid only for heredocs containing variables.
    </p>
    
    <div class="example" id="example-73">
     <p><strong>Example #1 Invalid 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">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOT<br /></span><span style="color: #DD0000">bar<br />&nbsp;&nbsp;&nbsp;&nbsp;EOT;<br />}<br />?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </div>

   <p class="para">
    Heredoc text behaves just like a double-quoted <span class="type"><a href="language.types.string.html" 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.html" class="type string">string</a></span>s.
   </p>

   <div class="example" id="example-74"> 
    <p><strong>Example #2 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&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOD<br /></span><span style="color: #DD0000">Example&nbsp;of&nbsp;string<br />spanning&nbsp;multiple&nbsp;lines<br />using&nbsp;heredoc&nbsp;syntax.<br /></span><span style="color: #007700">EOD;<br /><br /></span><span style="color: #FF8000">/*&nbsp;More&nbsp;complex&nbsp;example,&nbsp;with&nbsp;variables.&nbsp;*/<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Foo'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bar&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'Bar1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Bar2'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Bar3'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'MyName'</span><span style="color: #007700">;<br /><br />echo&nbsp;&lt;&lt;&lt;EOT<br /></span><span style="color: #DD0000">My&nbsp;name&nbsp;is&nbsp;"</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">".&nbsp;I&nbsp;am&nbsp;printing&nbsp;some&nbsp;</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,&nbsp;I&nbsp;am&nbsp;printing&nbsp;some&nbsp;</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&nbsp;should&nbsp;print&nbsp;a&nbsp;capital&nbsp;'A':&nbsp;\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>The above example will output:</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-75"> 
    <p><strong>Example #3 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">
    As of PHP 5.3.0, it&#039;s possible to initialize static variables and class 
    properties/constants using the Heredoc syntax:
   </p>

   <div class="example" id="example-76"> 
    <p><strong>Example #4 Using Heredoc to initialize static values</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">//&nbsp;Static&nbsp;variables<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;LABEL<br /></span><span style="color: #DD0000">Nothing&nbsp;in&nbsp;here...<br /></span><span style="color: #007700">LABEL;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Class&nbsp;properties/constants<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;</span><span style="color: #0000BB">BAR&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;FOOBAR<br /></span><span style="color: #DD0000">Constant&nbsp;example<br /></span><span style="color: #007700">FOOBAR;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$baz&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;FOOBAR<br /></span><span style="color: #DD0000">Property&nbsp;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">
    Starting with PHP 5.3.0, the opening Heredoc identifier may optionally be 
    enclosed in double quotes:
   </p>

   <div class="example" id="example-77"> 
    <p><strong>Example #5 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&nbsp;&lt;&lt;&lt;"FOOBAR"<br /></span><span style="color: #DD0000">Hello&nbsp;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 class="emphasis">no
    parsing 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
    <em>&lt;![CDATA[ ]]&gt;</em> 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 <em>&lt;&lt;&lt;</em>
    sequence used for heredocs, but the identifier which follows is enclosed in
    single quotes, e.g. <em>&lt;&lt;&lt;&#039;EOT&#039;</em>. 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-78">
    <p><strong>Example #6 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 />$str&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;'EOD'<br /></span><span style="color: #DD0000">Example&nbsp;of&nbsp;string<br />spanning&nbsp;multiple&nbsp;lines<br />using&nbsp;nowdoc&nbsp;syntax.<br /></span><span style="color: #007700">EOD;<br /><br /></span><span style="color: #FF8000">/*&nbsp;More&nbsp;complex&nbsp;example,&nbsp;with&nbsp;variables.&nbsp;*/<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Foo'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bar&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'Bar1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Bar2'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Bar3'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'MyName'</span><span style="color: #007700">;<br /><br />echo&nbsp;&lt;&lt;&lt;'EOT'<br /></span><span style="color: #DD0000">My&nbsp;name&nbsp;is&nbsp;"$name".&nbsp;I&nbsp;am&nbsp;printing&nbsp;some&nbsp;$foo-&gt;foo.<br />Now,&nbsp;I&nbsp;am&nbsp;printing&nbsp;some&nbsp;{$foo-&gt;bar[1]}.<br />This&nbsp;should&nbsp;not&nbsp;print&nbsp;a&nbsp;capital&nbsp;'A':&nbsp;\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>The above example will output:</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>
   
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Unlike heredocs, nowdocs can be used in any static data context. The
     typical example is initializing class properties or constants:
    </p>
   </p></blockquote>
    
   <div class="example" id="example-79">
    <p><strong>Example #7 Static data 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">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;&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>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Nowdoc support was added in PHP 5.3.0.
    </p>
   </p></blockquote>

  </div>

  <div class="sect3" id="language.types.string.parsing">
   <h4 class="title">Variable parsing</h4>

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

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

   <p class="simpara">
    The complex syntax can be recognised by the
    curly braces surrounding the expression.
   </p>

   <div class="sect4" id="language.types.string.parsing.simple">
    <h5 class="title">Simple syntax</h5>

    <p class="simpara">
     If a dollar sign (<em>$</em>) is encountered, the parser will
     greedily take as many tokens as possible to form a valid variable name.
     Enclose the variable name in curly braces to explicitly specify the end of
     the name.
    </p>

    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$juice&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"He&nbsp;drank&nbsp;some&nbsp;</span><span style="color: #0000BB">$juice</span><span style="color: #DD0000">&nbsp;juice."</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;Invalid.&nbsp;"s"&nbsp;is&nbsp;a&nbsp;valid&nbsp;character&nbsp;for&nbsp;a&nbsp;variable&nbsp;name,&nbsp;but&nbsp;the&nbsp;variable&nbsp;is&nbsp;$juice.<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"He&nbsp;drank&nbsp;some&nbsp;juice&nbsp;made&nbsp;of&nbsp;</span><span style="color: #0000BB">$juices</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">The above example will output:</p>
     <div class="example-contents screen">
<div class="cdata"><pre>
He drank some apple juice.
He drank some juice made of .
</pre></div>
     </div>
    </div>

    <p class="simpara">
     Similarly, an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> index or an <span class="type"><a href="language.types.object.html" class="type object">object</a></span> property
     can be parsed. With array indices, the closing square bracket
     (<em>]</em>) marks the end of the index. The same rules apply to
     object properties as to simple variables.
    </p>

    <div class="example" id="example-80"><p><strong>Example #8 Simple syntax example</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$juices&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"koolaid1"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"purple"</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"He&nbsp;drank&nbsp;some&nbsp;</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">&nbsp;juice."</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"He&nbsp;drank&nbsp;some&nbsp;</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">&nbsp;juice."</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"He&nbsp;drank&nbsp;some&nbsp;</span><span style="color: #0000BB">$juices</span><span style="color: #007700">[</span><span style="color: #0000BB">koolaid1</span><span style="color: #007700">]</span><span style="color: #DD0000">&nbsp;juice."</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />class&nbsp;</span><span style="color: #0000BB">people&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$john&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"John&nbsp;Smith"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$jane&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Jane&nbsp;Smith"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$robert&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Robert&nbsp;Paulsen"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$smith&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Smith"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$people&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">people</span><span style="color: #007700">();<br /><br />echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">john</span><span style="color: #DD0000">&nbsp;drank&nbsp;some&nbsp;</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">&nbsp;juice."</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">john</span><span style="color: #DD0000">&nbsp;then&nbsp;said&nbsp;hello&nbsp;to&nbsp;</span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">jane</span><span style="color: #DD0000">."</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">john</span><span style="color: #DD0000">'s&nbsp;wife&nbsp;greeted&nbsp;</span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">robert</span><span style="color: #DD0000">."</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">robert</span><span style="color: #DD0000">&nbsp;greeted&nbsp;the&nbsp;two&nbsp;</span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">smiths</span><span style="color: #DD0000">."</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Won't&nbsp;work<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>The above example will output:</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.
John Smith drank some apple juice.
John Smith then said hello to Jane Smith.
John Smith&#039;s wife greeted Robert Paulsen.
Robert Paulsen greeted the two .
</pre></div>
     </div>
    </div>

    <p class="simpara">
     For anything more complex, you should use the complex syntax.
    </p>
   </div>

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

    <p class="simpara">
     This isn&#039;t called complex because the syntax is complex, but because it
     allows for the use of complex expressions.
    </p>

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

    <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: #FF8000">//&nbsp;Show&nbsp;all&nbsp;errors<br /></span><span style="color: #0000BB">error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$great&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'fantastic'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Won't&nbsp;work,&nbsp;outputs:&nbsp;This&nbsp;is&nbsp;{&nbsp;fantastic}<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;{&nbsp;</span><span style="color: #0000BB">$great</span><span style="color: #DD0000">}"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Works,&nbsp;outputs:&nbsp;This&nbsp;is&nbsp;fantastic<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;</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">;<br />echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;</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">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Works<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;square&nbsp;is&nbsp;</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&nbsp;centimeters&nbsp;broad."</span><span style="color: #007700">;&nbsp;<br /><br /><br /></span><span style="color: #FF8000">//&nbsp;Works,&nbsp;quoted&nbsp;keys&nbsp;only&nbsp;work&nbsp;using&nbsp;the&nbsp;curly&nbsp;brace&nbsp;syntax<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;works:&nbsp;</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">;<br /><br /><br /></span><span style="color: #FF8000">//&nbsp;Works<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;works:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">4</span><span style="color: #007700">][</span><span style="color: #0000BB">3</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;This&nbsp;is&nbsp;wrong&nbsp;for&nbsp;the&nbsp;same&nbsp;reason&nbsp;as&nbsp;$foo[bar]&nbsp;is&nbsp;wrong&nbsp;&nbsp;outside&nbsp;a&nbsp;string.<br />//&nbsp;In&nbsp;other&nbsp;words,&nbsp;it&nbsp;will&nbsp;still&nbsp;work,&nbsp;but&nbsp;only&nbsp;because&nbsp;PHP&nbsp;first&nbsp;looks&nbsp;for&nbsp;a<br />//&nbsp;constant&nbsp;named&nbsp;foo;&nbsp;an&nbsp;error&nbsp;of&nbsp;level&nbsp;E_NOTICE&nbsp;(undefined&nbsp;constant)&nbsp;will&nbsp;be<br />//&nbsp;thrown.<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;wrong:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">foo</span><span style="color: #007700">][</span><span style="color: #0000BB">3</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;<br /><br /></span><span style="color: #FF8000">//&nbsp;Works.&nbsp;When&nbsp;using&nbsp;multi-dimensional&nbsp;arrays,&nbsp;always&nbsp;use&nbsp;braces&nbsp;around&nbsp;arrays<br />//&nbsp;when&nbsp;inside&nbsp;of&nbsp;strings<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;works:&nbsp;</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">3</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Works.<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;works:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</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">3</span><span style="color: #007700">];<br /><br />echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;works&nbsp;too:&nbsp;</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">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;the&nbsp;value&nbsp;of&nbsp;the&nbsp;var&nbsp;named&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #007700">{${</span><span style="color: #0000BB">$name</span><span style="color: #007700">}}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;the&nbsp;value&nbsp;of&nbsp;the&nbsp;var&nbsp;named&nbsp;by&nbsp;the&nbsp;return&nbsp;value&nbsp;of&nbsp;getName():&nbsp;</span><span style="color: #007700">{${</span><span style="color: #0000BB">getName</span><span style="color: #007700">()}}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;the&nbsp;value&nbsp;of&nbsp;the&nbsp;var&nbsp;named&nbsp;by&nbsp;the&nbsp;return&nbsp;value&nbsp;of&nbsp;\$object-&gt;getName():&nbsp;</span><span style="color: #007700">{${</span><span style="color: #0000BB">$object</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">()}}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Won't&nbsp;work,&nbsp;outputs:&nbsp;This&nbsp;is&nbsp;the&nbsp;return&nbsp;value&nbsp;of&nbsp;getName():&nbsp;{getName()}<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;the&nbsp;return&nbsp;value&nbsp;of&nbsp;getName():&nbsp;{getName()}"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>

     </div>

    </div>

    <p class="para">
     It is also possible to access class properties using variables
     within strings using this syntax.
    </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'I&nbsp;am&nbsp;bar.'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$baz&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'quux'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"</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: #DD0000">\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #007700">{</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$baz</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   <p class="para">The above example will output:</p>
   <div class="example-contents screen">
<div class="cdata"><pre>
I am bar.
I am bar.
</pre></div>
   </div>
   </div>
    
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      Functions, method calls, static class variables, and class
      constants inside <em>{$}</em> work since PHP
      5. However, the value accessed will be interpreted as the name
      of a variable in the scope in which the string is defined. Using
      single curly braces (<em>{}</em>) will not work for
      accessing the return values of functions or methods or the
      values of class constants or static class variables.
     </p>
    </p></blockquote>

    <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: #FF8000">//&nbsp;Show&nbsp;all&nbsp;errors.<br /></span><span style="color: #0000BB">error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /><br />class&nbsp;</span><span style="color: #0000BB">beers&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;</span><span style="color: #0000BB">softdrink&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'rootbeer'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;</span><span style="color: #0000BB">$ale&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'ipa'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$rootbeer&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'A&nbsp;&amp;&nbsp;W'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$ipa&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Alexander&nbsp;Keith\'s'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;This&nbsp;works;&nbsp;outputs:&nbsp;I'd&nbsp;like&nbsp;an&nbsp;A&nbsp;&amp;&nbsp;W<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"I'd&nbsp;like&nbsp;an&nbsp;</span><span style="color: #007700">{${</span><span style="color: #0000BB">beers</span><span style="color: #007700">::</span><span style="color: #0000BB">softdrink</span><span style="color: #007700">}}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;This&nbsp;works&nbsp;too;&nbsp;outputs:&nbsp;I'd&nbsp;like&nbsp;an&nbsp;Alexander&nbsp;Keith's<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"I'd&nbsp;like&nbsp;an&nbsp;</span><span style="color: #007700">{${</span><span style="color: #0000BB">beers</span><span style="color: #007700">::</span><span style="color: #0000BB">$ale</span><span style="color: #007700">}}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>

   </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.html" 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.html" class="type string">string</a></span> using square <span class="type"><a href="language.types.array.html" class="type array">array</a></span> brackets, as in
    <var class="varname"><var class="varname">$str[42]</var></var>. Think of a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> as an
    <span class="type"><a href="language.types.array.html" class="type array">array</a></span> of characters for this purpose. The functions
     <span class="function"><a href="function.substr.html" class="function">substr()</a></span> and  <span class="function"><a href="function.substr-replace.html" 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">Note</strong>: 
    <span class="simpara">
     <span class="type"><a href="language.types.string.html" class="type String">String</a></span>s may also be accessed using braces, as in
     <var class="varname"><var class="varname">$str{42}</var></var>, for the same purpose.
    </span>
   </p></blockquote>

   <div class="warning"><strong class="warning">Warning</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>E_NOTICE</code></strong>.
     Negative offset emits <strong><code>E_NOTICE</code></strong> in write but reads empty string.
     Only the first character of an assigned string is used.
     Assigning empty string assigns NULL byte.
    </p>
   </div>

   <div class="warning"><strong class="warning">Warning</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>

   <div class="example" id="example-81">
    <p><strong>Example #9 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">//&nbsp;Get&nbsp;the&nbsp;first&nbsp;character&nbsp;of&nbsp;a&nbsp;string<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'This&nbsp;is&nbsp;a&nbsp;test.'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$first&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">//&nbsp;Get&nbsp;the&nbsp;third&nbsp;character&nbsp;of&nbsp;a&nbsp;string<br /></span><span style="color: #0000BB">$third&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">//&nbsp;Get&nbsp;the&nbsp;last&nbsp;character&nbsp;of&nbsp;a&nbsp;string.<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'This&nbsp;is&nbsp;still&nbsp;a&nbsp;test.'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$last&nbsp;</span><span style="color: #007700">=&nbsp;</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">];&nbsp;<br /><br /></span><span style="color: #FF8000">//&nbsp;Modify&nbsp;the&nbsp;last&nbsp;character&nbsp;of&nbsp;a&nbsp;string<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Look&nbsp;at&nbsp;the&nbsp;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">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'e'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   
   <p class="para">
    As of PHP 5.4 string offsets have to either be integers or integer-like strings, otherwise a warning
    will be thrown. Previously an offset like <em>&quot;foo&quot;</em> was silently cast to <em>0</em>.
   </p>

   <div class="example" id="example-82">
    <p><strong>Example #10 Differences between PHP 5.3 and PHP 5.4</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'abc'</span><span style="color: #007700">;<br /><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: #DD0000">'1'</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: #DD0000">'1'</span><span style="color: #007700">]));<br /><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: #DD0000">'1.0'</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: #DD0000">'1.0'</span><span style="color: #007700">]));<br /><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: #DD0000">'x'</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: #DD0000">'x'</span><span style="color: #007700">]));<br /><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: #DD0000">'1x'</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: #DD0000">'1x'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>Output of the above example in PHP 5.3:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(1) &quot;b&quot;
bool(true)
string(1) &quot;b&quot;
bool(true)
string(1) &quot;a&quot;
bool(true)
string(1) &quot;b&quot;
bool(true)
</pre></div>
    </div>
    <div class="example-contents"><p>Output of the above example in PHP 5.4:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(1) &quot;b&quot;
bool(true)

Warning: Illegal string offset &#039;1.0&#039; in /tmp/t.php on line 7
string(1) &quot;b&quot;
bool(false)

Warning: Illegal string offset &#039;x&#039; in /tmp/t.php on line 9
string(1) &quot;a&quot;
bool(false)
string(1) &quot;b&quot;
bool(false)
</pre></div>
    </div>
   </div>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Accessing variables of other types (not including arrays or objects
     implementing the appropriate interfaces) using <em>[]</em> or
     <em>{}</em> silently returns <strong><code>NULL</code></strong>.
    </p>
   </p></blockquote>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     PHP 5.5 added support for accessing characters within string literals
     using <em>[]</em> or <em>{}</em>.
    </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.html" 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 class="emphasis">not</em> work for this.
   See <a href="language.operators.string.html" 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.html" class="type string">string</a></span> manipulation.
  </p>

  <p class="simpara">
   See the <a href="ref.strings.html" class="link">string functions section</a> for
   general functions, and the <a href="ref.regex.html" class="link">regular expression
   functions</a> or the <a href="ref.pcre.html" 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.html" class="link">functions for URL strings</a>, and
   functions to encrypt/decrypt strings
   (<a href="ref.mcrypt.html" class="link">mcrypt</a> and
   <a href="ref.mhash.html" class="link">mhash</a>).
  </p>

  <p class="simpara">
   Finally, see also the <a href="ref.ctype.html" 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.html" class="type string">string</a></span> using the
   <em>(string)</em> cast or the  <span class="function"><a href="function.strval.html" class="function">strval()</a></span> function.
   <span class="type"><a href="language.types.string.html" 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.html" class="type string">string</a></span> is needed. This happens when using the
    <span class="function"><a href="function.echo.html" class="function">echo</a></span> or  <span class="function"><a href="function.print.html" class="function">print</a></span> functions, or when a
   variable is compared to a <span class="type"><a href="language.types.string.html" class="type string">string</a></span>. The sections on
   <a href="language.types.html" class="link">Types</a> and
   <a href="language.types.type-juggling.html" class="link">Type Juggling</a> will make
   the following clearer. See also the  <span class="function"><a href="function.settype.html" class="function">settype()</a></span> function.
  </p>
  
  <p class="para">
   A <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> <strong><code>TRUE</code></strong> value is converted to the <span class="type"><a href="language.types.string.html" class="type string">string</a></span>
   <em>&quot;1&quot;</em>. <span class="type"><a href="language.types.boolean.html" class="type Boolean">Boolean</a></span> <strong><code>FALSE</code></strong> is converted to
   <em>&quot;&quot;</em> (the empty string). This allows conversion back and
   forth between <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> and <span class="type"><a href="language.types.string.html" class="type string">string</a></span> values.
  </p>

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

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    The decimal point character is defined in the script&#039;s locale (category
    LC_NUMERIC). See the  <span class="function"><a href="function.setlocale.html" class="function">setlocale()</a></span> function.
   </p>
  </p></blockquote>

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

  <p class="para">
   <span class="type"><a href="language.types.object.html" class="type Object">Object</a></span>s in PHP 4 are always converted to the <span class="type"><a href="language.types.string.html" class="type string">string</a></span>
   <em>&quot;Object&quot;</em>. To print the values of object properties for
   debugging reasons, read the paragraphs below. To get an object&#039;s class name,
   use the  <span class="function"><a href="function.get-class.html" class="function">get_class()</a></span> function. As of PHP 5, the
   <a href="language.oop5.magic.html" class="link">__toString</a> method is used when
   applicable.
  </p>

  <p class="para">
   <span class="type"><a href="language.types.resource.html" class="type Resource">Resource</a></span>s are always converted to <span class="type"><a href="language.types.string.html" class="type string">string</a></span>s with the
   structure <em>&quot;Resource id #1&quot;</em>, where <em>1</em> is
   the unique number assigned to the <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> by PHP at runtime. Do
   not rely upon this structure; it is subject to change. To get a
   <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>&#039;s type, use the
    <span class="function"><a href="function.get-resource-type.html" class="function">get_resource_type()</a></span> function.
  </p>

  <p class="para">
   <strong><code>NULL</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.html" class="type array">array</a></span>,
   <span class="type"><a href="language.types.object.html" class="type object">object</a></span>, or <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> to a <span class="type"><a href="language.types.string.html" 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.html" class="function">print_r()</a></span> and  <span class="function"><a href="function.var-dump.html" 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.html" 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.html" class="function">serialize()</a></span> function. If the PHP engine was built with
   <a href="ref.wddx.html" class="link">WDDX</a> support, PHP values can also be
   serialized as well-formed XML text.
  </p>

 </div>

 <div class="sect2" id="language.types.string.conversion">
  <h3 class="title">String conversion to numbers</h3>

  <p class="simpara">
   When a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> is evaluated in a numeric context, the resulting
   value and type are determined as follows.
  </p>

  <p class="simpara">
   If the <span class="type"><a href="language.types.string.html" class="type string">string</a></span> does not contain any of the characters &#039;.&#039;, &#039;e&#039;,
   or &#039;E&#039; and the numeric value fits into integer type limits (as defined by
   <strong><code>PHP_INT_MAX</code></strong>), the <span class="type"><a href="language.types.string.html" class="type string">string</a></span> will be evaluated
   as an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>. In all other cases it will be evaluated as a
   <span class="type"><a href="language.types.float.html" class="type float">float</a></span>.
  </p>

  <p class="para">
   The value is given by the initial portion of the <span class="type"><a href="language.types.string.html" class="type string">string</a></span>. If the
   <span class="type"><a href="language.types.string.html" class="type string">string</a></span> starts with valid numeric data, this will be the value
   used. Otherwise, the value will be 0 (zero). Valid numeric data is an
   optional sign, followed by one or more digits (optionally containing a
   decimal point), followed by an optional exponent. The exponent is an &#039;e&#039; or
   &#039;E&#039; followed by one or more digits.
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"10.5"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;float&nbsp;(11.5)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"-1.3e3"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;float&nbsp;(-1299)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"bob-1.3e3"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;integer&nbsp;(1)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"bob3"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;integer&nbsp;(1)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"10&nbsp;Small&nbsp;Pigs"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;integer&nbsp;(11)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #DD0000">"10.2&nbsp;Little&nbsp;Piggies"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;float&nbsp;(14.2)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"10.0&nbsp;pigs&nbsp;"&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;float&nbsp;(11)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"10.0&nbsp;pigs&nbsp;"&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">1.0</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;float&nbsp;(11)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="simpara">
   For more information on this conversion, see the Unix manual page for
   strtod(3).
  </p>

  <p class="para">
   To test any of the examples in this section, cut and paste the examples and
   insert the following line to see what&#039;s going on:
  </p>

  <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&nbsp;</span><span style="color: #DD0000">"\$foo==</span><span style="color: #0000BB">$foo</span><span style="color: #DD0000">;&nbsp;type&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">gettype&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   Do not expect to get the code of one character by converting it to integer,
   as is done in C. Use the  <span class="function"><a href="function.ord.html" class="function">ord()</a></span> and
    <span class="function"><a href="function.chr.html" class="function">chr()</a></span> functions to convert between ASCII codes and
   characters.
  </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.html" 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 <em>0</em> (“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
   <em>&quot;á&quot;</em> equivalent to <em>&quot;\xE1&quot;</em> (ISO-8859-1),
   <em>&quot;\xC3\xA1&quot;</em> (UTF-8, C form),
   <em>&quot;\x61\xCC\x81&quot;</em> (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 explicity 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.html" class="function">substr()</a></span>, 
      <span class="function"><a href="function.strpos.html" class="function">strpos()</a></span>,  <span class="function"><a href="function.strlen.html" class="function">strlen()</a></span> or
      <span class="function"><a href="function.strcmp.html" 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.html" class="function">htmlentities()</a></span> and the majority of the
     functions in the <a href="book.mbstring.html" 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.html" class="function">setlocale()</a></span>), but
     operate byte-by-byte. This is the case of  <span class="function"><a href="function.strcasecmp.html" class="function">strcasecmp()</a></span>,
      <span class="function"><a href="function.strtoupper.html" class="function">strtoupper()</a></span> and  <span class="function"><a href="function.ucfirst.html" class="function">ucfirst()</a></span>.
     This means they can be used only with single-byte encodings, as long as
     the encoding is matched by the locale. For instance
     <em>strtoupper(&quot;á&quot;)</em> may return <em>&quot;Á&quot;</em> if the
     locale is correctly set and <em>á</em> is encoded with a single
     byte. If it is encoded in UTF-8, the correct result will not be returned
     and the resulting string may or may not be returned corrupted, depending
     on the current locale.
    </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.html" class="link">intl</a> extension and in the
     <a href="book.pcre.html" class="link">PCRE</a> extension
     (in the last case, only when the <em>u</em> modifier is used).
     Although this is due to their special purpose, the function
      <span class="function"><a href="function.utf8-decode.html" class="function">utf8_decode()</a></span> assumes a UTF-8 encoding and the
     function  <span class="function"><a href="function.utf8-encode.html" class="function">utf8_encode()</a></span> assumes an ISO-8859-1 encoding.
    </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.html" class="link">intl</a> and
   <a href="book.mbstring.html" 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><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.types.float.html">Floating point numbers</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.types.array.html">Arrays</a></div>
 <div class="up"><a href="language.types.html">Types</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>