⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 language.types.string.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 4 页
字号:
       </td>      </tr>     </tbody>    </colgroup>   </table>   <p class="para">    As in single quoted <a href="language.types.string.html" class="type string">string</a>s, escaping any other character will    result in the backslash being printed too. Before PHP 5.1.1, the backslash    in <i>\{$var}</i> was not been printed.   </p>   <p class="para">    The most important feature of double-quoted <a href="language.types.string.html" class="type string">string</a>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 id="language.types.string.syntax.heredoc" class="sect3">   <h4 class="title">Heredoc</h4>   <p class="simpara">    A third way to delimit <a href="language.types.string.html" class="type string">string</a>s is the heredoc syntax:    <i>&lt;&lt;&lt;</i>. After this operator, an identifier is    provided, then a newline. The <a href="language.types.string.html" class="type string">string</a> 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"><b class="warning">Warning</b>    <p class="simpara">     It is very important to note that the line with the closing identifier must     contain no other characters, except <em class="emphasis">possibly</em> a     semicolon (<i>;</i>). 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 <i>\n</i> on     UNIX systems, including Mac OS X. The closing delimiter (possibly followed     by a semicolon) 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 members. Use     <a href="language.types.string.html#language.types.string.syntax.nowdoc" class="link">nowdocs</a> instead.    </p>        <div class="example">     <p><b>Example #1 Invalid example</b></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: #0000BB">bar<br /></span><span style="color: #007700">EOT;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>     </div>    </div>   </div>   <p class="para">    Heredoc text behaves just like a double-quoted <a href="language.types.string.html" class="type string">string</a>, 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 <a href="language.types.string.html" class="type string">string</a>s.   </p>   <div class="example">     <p><b>Example #2 Heredoc string quoting example</b></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: #0000BB">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: #0000BB">My&nbsp;name&nbsp;is&nbsp;"$name".&nbsp;I&nbsp;am&nbsp;printing&nbsp;some&nbsp;$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo.<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: #0000BB">.<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>   <p class="para">The above example will output:</p>   <div class="example-contents"><pre><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>   </pre></div>   <p class="para">    Its also possible to use the Heredoc syntax to pass data to function     arguments:   </p>   <div class="example">     <p><b>Example #3 Heredoc in arguments example</b></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: #0000BB">foobar!<br /></span><span style="color: #007700">EOD<br />));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>   <blockquote><p><b class="note">Note</b>:          Heredoc support was added in PHP 4.    <br />   </p></blockquote>  </div>    <div id="language.types.string.syntax.nowdoc" class="sect3">   <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    <i>&lt;![CDATA[ ]]&gt;</i> 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 <i>&lt;&lt;&lt;</i>    seqeuence used for heredocs, but the identifier which follows is enclosed in    single quotes, e.g. <i>&lt;&lt;&lt;&#039;EOT&#039;</i>. All the rules for    heredoc identifiers also apply to nowdoc identifiers, especially those    regarding the appearance of the closing identifier.   </p>      <div class="example">    <p><b>Example #4 Nowdoc string quoting example</b></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;</span><span style="color: #DD0000">'EOD'<br /></span><span style="color: #0000BB">Example&nbsp;of&nbsp;string<br />spanning&nbsp;multiple&nbsp;lines<br />using&nbsp;nowdoc&nbsp;syntax</span><span style="color: #007700">.<br /></span><span style="color: #0000BB">EOD</span><span style="color: #007700">;<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;</span><span style="color: #DD0000">'EOT'<br /></span><span style="color: #0000BB">My&nbsp;name&nbsp;is&nbsp;</span><span style="color: #DD0000">"$name"</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">I&nbsp;am&nbsp;printing&nbsp;some&nbsp;$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">.<br /></span><span style="color: #0000BB">Now</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">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">]}.<br /></span><span style="color: #0000BB">This&nbsp;should&nbsp;not&nbsp;</span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">a&nbsp;capital&nbsp;</span><span style="color: #DD0000">'A'</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">x41<br />EOT</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>   <p class="para">The above example will output:</p>   <div class="example-contents"><pre><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></pre></div>      <blockquote><p><b class="note">Note</b>:          Unlike heredocs, nowdocs can be used in any static data context. The     typical example is initializing class members or constants:    <br />        <div class="example">     <p><b>Example #5 Static data example</b></p>     <div class="example-contents">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -