📄 language.types.array.html
字号:
way to traverse an <a href="language.types.array.html" class="type array">array</a>. </p> </div> <div id="language.types.array.donts" class="sect2"> <h3 class="title">Array do's and don'ts</h3> <div id="language.types.array.foo-bar" class="sect3"> <h4 class="title">Why is <i>$foo[bar]</i> wrong?</h4> <p class="para"> Always use quotes around a string literal array index. For example, <i>$foo['bar']</i> is correct, while <i>$foo[bar]</i> is not. But why? It is common to encounter this kind of syntax in old scripts: </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$foo</span><span style="color: #007700">[</span><span style="color: #0000BB">bar</span><span style="color: #007700">] = </span><span style="color: #DD0000">'enemy'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$foo</span><span style="color: #007700">[</span><span style="color: #0000BB">bar</span><span style="color: #007700">];<br /></span><span style="color: #FF8000">// etc<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a <a href="language.types.string.html" class="type string">string</a> ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a <em class="emphasis">bare string</em> (an unquoted <a href="language.types.string.html" class="type string">string</a> which does not correspond to any known symbol) into a <a href="language.types.string.html" class="type string">string</a> which contains the bare <a href="language.types.string.html" class="type string">string</a>. For instance, if there is no defined constant named <b><tt>bar</tt></b>, then PHP will substitute in the <a href="language.types.string.html" class="type string">string</a> <i>'bar'</i> and use that. </p> <blockquote><p><b class="note">Note</b>: <span class="simpara"> This does not mean to <em class="emphasis">always</em> quote the key. Do not quote keys which are <a href="language.constants.html" class="link">constants</a> or <a href="language.variables.html" class="link">variables</a>, as this will prevent PHP from interpreting them. </span> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'display_errors'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'html_errors'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// Simple array:<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$count </span><span style="color: #007700">= </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br />for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">< </span><span style="color: #0000BB">$count</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br /> echo </span><span style="color: #DD0000">"\nChecking $i: \n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Bad: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">'$i'</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Good: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Bad: {$array['$i']}\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Good: {$array[$i]}\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> The above example will output:<br /> <div class="example-contents"><pre><div class="cdata"><pre>Checking 0: Notice: Undefined index: $i in /path/to/script.html on line 9Bad: Good: 1Notice: Undefined index: $i in /path/to/script.html on line 11Bad: Good: 1Checking 1: Notice: Undefined index: $i in /path/to/script.html on line 9Bad: Good: 2Notice: Undefined index: $i in /path/to/script.html on line 11Bad: Good: 2</pre></div> </pre></div> </p></blockquote> <p class="para"> More examples to demonstrate this behaviour: </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">$arr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'fruit' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">, </span><span style="color: #DD0000">'veggie' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'carrot'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Correct<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// apple<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'veggie'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// carrot<br /><br />// Incorrect. This works but also throws a PHP error of level E_NOTICE because<br />// of an undefined constant named fruit<br />// <br />// Notice: Use of undefined constant fruit - assumed 'fruit' in...<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">]; </span><span style="color: #FF8000">// apple<br /><br />// This defines a constant to demonstrate what's going on. The value 'veggie'<br />// is assigned to a constant named fruit.<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">, </span><span style="color: #DD0000">'veggie'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Notice the difference now<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// apple<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">]; </span><span style="color: #FF8000">// carrot<br /><br />// The following is okay, as it's inside a string. Constants are not looked for<br />// within strings, so no E_NOTICE occurs here<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"Hello $arr[fruit]"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Hello apple<br /><br />// With one exception: braces surrounding arrays within strings allows constants<br />// to be interpreted<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"Hello {$arr[fruit]}"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Hello carrot<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"Hello {$arr['fruit']}"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Hello apple<br /><br />// This will not work, and will result in a parse error, such as:<br />// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'<br />// This of course applies to using superglobals in strings as well<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"Hello $arr['fruit']"</span><span style="color: #007700">;<br />print </span><span style="color: #DD0000">"Hello $_GET['foo']"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Concatenation is another option<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"Hello " </span><span style="color: #007700">. </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">]; </span><span style="color: #FF8000">// Hello apple<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> When <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> is set to show <b><tt>E_NOTICE</tt></b> level errors (by setting it to <b><tt>E_ALL</tt></b>, for example), such uses will become immediately visible. By default, <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> is set not to show notices. </p> <p class="para"> As stated in the <a href="language.types.array.html#language.types.array.syntax" class="link">syntax</a> section, what's inside the square brackets ('<i>[</i>' and '<i>]</i>') must be an expression. This means that code like this works: </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: #007700">echo </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">somefunc</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">)];<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> This is an example of using a function return value as the array index. PHP also knows about constants: </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -