📄 language.types.string.html
字号:
</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><<<</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'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 "clean", 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"><?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">= <<<EOT<br /></span><span style="color: #0000BB">bar<br /></span><span style="color: #007700">EOT;<br />}<br /></span><span style="color: #0000BB">?></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"><?php<br />$str </span><span style="color: #007700">= <<<EOD<br /></span><span style="color: #0000BB">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">foo</span><span style="color: #007700">()<br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></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">-></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 <<<EOT<br /></span><span style="color: #0000BB">My name is "$name". I am printing some $foo</span><span style="color: #007700">-></span><span style="color: #0000BB">foo.<br />Now, I am printing some </span><span style="color: #007700">{</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-></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 should print a capital 'A': \x41<br /></span><span style="color: #007700">EOT;<br /></span><span style="color: #0000BB">?></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 "MyName". I am printing some Foo.Now, I am printing some Bar2.This should print a capital 'A': 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"><?php<br />var_dump</span><span style="color: #007700">(array(<<<EOD<br /></span><span style="color: #0000BB">foobar!<br /></span><span style="color: #007700">EOD<br />));<br /></span><span style="color: #0000BB">?></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><![CDATA[ ]]></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><<<</i> seqeuence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <i><<<'EOT'</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"><?php<br />$str </span><span style="color: #007700">= <<<</span><span style="color: #DD0000">'EOD'<br /></span><span style="color: #0000BB">Example of string<br />spanning multiple lines<br />using nowdoc 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">/* 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 /> 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">foo</span><span style="color: #007700">()<br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></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">-></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 <<<</span><span style="color: #DD0000">'EOT'<br /></span><span style="color: #0000BB">My name is </span><span style="color: #DD0000">"$name"</span><span style="color: #007700">. </span><span style="color: #0000BB">I am printing some $foo</span><span style="color: #007700">-></span><span style="color: #0000BB">foo</span><span style="color: #007700">.<br /></span><span style="color: #0000BB">Now</span><span style="color: #007700">, </span><span style="color: #0000BB">I am printing some </span><span style="color: #007700">{</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-></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 should not </span><span style="color: #007700">print </span><span style="color: #0000BB">a capital </span><span style="color: #DD0000">'A'</span><span style="color: #007700">: </span><span style="color: #0000BB">x41<br />EOT</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></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 "$name". I am printing some $foo->foo.Now, I am printing some {$foo->bar[1]}.This should not print a capital 'A': \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 + -