📄 language.types.string.html
字号:
<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">= <<<</span><span style="color: #DD0000">'EOT'<br /></span><span style="color: #0000BB">bar<br />EOT</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p></blockquote> <blockquote><p><b class="note">Note</b>: Nowdoc support was added in PHP 5.3.0. <br /> </p></blockquote> </div> <div id="language.types.string.parsing" class="sect3"> <h4 class="title">Variable parsing</h4> <p class="simpara"> When a <a href="language.types.string.html" class="type string">string</a> 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 <a href="language.types.array.html" class="type array">array</a> value, or an <a href="language.types.object.html" class="type object">object</a> property in a <a href="language.types.string.html" class="type string">string</a> with a minimum of effort. </p> <p class="simpara"> The complex syntax was introduced in PHP 4, and can be recognised by the curly braces surrounding the expression. </p> <div id="language.types.string.parsing.simple" class="sect4"> <h5 class="title">Simple syntax</h5> <p class="simpara"> If a dollar sign (<i>$</i>) 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"><?php<br />$beer </span><span style="color: #007700">= </span><span style="color: #DD0000">'Heineken'</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"$beer's taste is great"</span><span style="color: #007700">; </span><span style="color: #FF8000">// works; "'" is an invalid character for variable names<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"He drank some $beers"</span><span style="color: #007700">; </span><span style="color: #FF8000">// won't work; 's' is a valid character for variable names but the variable is "$beer"<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"He drank some ${beer}s"</span><span style="color: #007700">; </span><span style="color: #FF8000">// works<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"He drank some {$beer}s"</span><span style="color: #007700">; </span><span style="color: #FF8000">// works<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="simpara"> Similarly, an <a href="language.types.array.html" class="type array">array</a> index or an <a href="language.types.object.html" class="type object">object</a> property can be parsed. With array indices, the closing square bracket (<i>]</i>) marks the end of the index. The same rules apply to object properties as to simple variables. </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// These examples are specific to using arrays inside of strings.<br />// When outside of a string, always quote array string keys and do not use<br />// {braces}.<br /><br />// Show all 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">$fruits </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'strawberry' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'red'</span><span style="color: #007700">, </span><span style="color: #DD0000">'banana' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Works, but note that this works differently outside a string<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"A banana is $fruits[banana]."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Works<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"A banana is {$fruits['banana']}."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Works, but PHP looks for a constant named banana first, as described below.<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"A banana is {$fruits[banana]}."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Won't work, use braces. This results in a parse error.<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"A banana is $fruits['banana']."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Works<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"A banana is " </span><span style="color: #007700">. </span><span style="color: #0000BB">$fruits</span><span style="color: #007700">[</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">] . </span><span style="color: #DD0000">"."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Works<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This square is $square->width meters broad."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Won't work. For a solution, see the complex syntax.<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This square is $square->width00 centimeters broad."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="simpara"> For anything more complex, you should use the complex syntax. </p> </div> <div id="language.types.string.parsing.complex" class="sect4"> <h5 class="title">Complex (curly) syntax</h5> <p class="simpara"> This isn't called complex because the syntax is complex, but because it allows for the use of complex expressions. </p> <p class="simpara"> In fact, any value in the namespace can be included in a <a href="language.types.string.html" class="type string">string</a> with this syntax. Simply write the expression the same way as it would appeared outside the <a href="language.types.string.html" class="type string">string</a>, and then wrap it in <i>{</i> and <i>}</i>. Since <i>{</i> can not be escaped, this syntax will only be recognised when the <i>$</i> immediately follows the <i>{</i>. Use <i>{\$</i> to get a literal <i>{$</i>. 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"><?php<br /></span><span style="color: #FF8000">// Show all 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 </span><span style="color: #007700">= </span><span style="color: #DD0000">'fantastic'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Won't work, outputs: This is { fantastic}<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This is { $great}"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Works, outputs: This is fantastic<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This is {$great}"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"This is ${great}"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Works<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This square is {$square->width}00 centimeters broad."</span><span style="color: #007700">; <br /><br /></span><span style="color: #FF8000">// Works<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This works: {$arr[4][3]}"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// This is wrong for the same reason as $foo[bar] is wrong outside a string.<br />// In other words, it will still work, but only because PHP first looks for a<br />// constant named foo; an error of level E_NOTICE (undefined constant) will be<br />// thrown.<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This is wrong: {$arr[foo][3]}"</span><span style="color: #007700">; <br /><br /></span><span style="color: #FF8000">// Works. When using multi-dimensional arrays, always use braces around arrays<br />// when inside of strings<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This works: {$arr['foo'][3]}"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Works.<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"This works: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">][</span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /><br />echo </span><span style="color: #DD0000">"This works too: {$obj->values[3]->name}"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"This is the value of the var named $name: {${$name}}"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"This is the value of the var named by the return value of getName(): {${getName()}}"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <blockquote><p><b class="note">Note</b>: Functions and method calls inside <i>{$}</i> work since PHP 5. <br /> </p></blockquote> </div> </div> <div id="language.types.string.substr" class="sect3"> <h4 class="title">String access and modification by character</h4> <p class="para"> Characters within <a href="language.types.string.html" class="type string">string</a>s may be accessed and modified by specifying the zero-based offset of the desired character after the <a href="language.types.string.html" class="type string">string</a> using square <a href="language.types.array.html" class="type array">array</a> brackets, as in <var class="varname">$str[42]</var>. Think of a <a href="language.types.string.html" class="type string">string</a> as an <a href="language.types.array.html" class="type array">array</a> of characters for this purpose. </p> <blockquote><p><b class="note">Note</b>: <span class="simpara"> <a href="language.types.string.html" class="type String">String</a>s may also be accessed using braces, as in <var class="varname">$str{42}</var>, for the same purpose. However, this syntax is deprecated as of PHP 6. Use square brackets instead. </span> </p></blockquote> <div class="example"> <p><b>Example #6 Some string examples</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Get the first character of a string<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'This is a test.'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$first </span><span style="color: #007700">= </span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Get the third character of a string<br /></span><span style="color: #0000BB">$third </span><span style="color: #007700">= </span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Get the last character of a string.<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'This is still a test.'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$last </span><span style="color: #007700">= </span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">)-</span><span style="color: #0000BB">1</span><span style="color: #007700">]; <br /><br /></span><span style="color: #FF8000">// Modify the last character of a string<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'Look at the sea'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$str</span><span style="color: #007700">[</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">)-</span><span style="color: #0000BB">1</span><span style="color: #007700">] = </span><span style="color: #DD0000">'e'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <blockquote><p><b class="note">Note</b>: Accessing variables of other types using <i>[]</i> or <i>{}</i> silently returns <b><tt>NULL</tt></b>. <br /> </p></blockquote> </div> </div> <div id="language.types.string.useful-funcs" class="sect2"> <h3 class="title">Useful functions and operators</h3> <p class="para"> <a href="language.types.string.html" class="type String">String</a>s may be concatenated using the '.' (dot) operator. Note that the '+' (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 <a href="language.types.string.html" class="type string">string</a> manipulation. </p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -