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

📄 language.types.array.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 5 页
字号:
   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&#039;s and don&#039;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[&#039;bar&#039;]</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">&lt;?php<br />$foo</span><span style="color: #007700">[</span><span style="color: #0000BB">bar</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'enemy'</span><span style="color: #007700">;<br />echo&nbsp;</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">//&nbsp;etc<br /></span><span style="color: #0000BB">?&gt;</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> (&#039;bar&#039; - 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>&#039;bar&#039;</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">&lt;?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">,&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;Simple&nbsp;array:<br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$count&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br />for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$count</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\nChecking&nbsp;$i:&nbsp;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Bad:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #DD0000">'$i'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Good:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Bad:&nbsp;{$array['$i']}\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Good:&nbsp;{$array[$i]}\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</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">&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">$arr&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'fruit'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'veggie'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'carrot'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Correct<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">];&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;apple<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'veggie'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;carrot<br /><br />//&nbsp;Incorrect.&nbsp;&nbsp;This&nbsp;works&nbsp;but&nbsp;also&nbsp;throws&nbsp;a&nbsp;PHP&nbsp;error&nbsp;of&nbsp;level&nbsp;E_NOTICE&nbsp;because<br />//&nbsp;of&nbsp;an&nbsp;undefined&nbsp;constant&nbsp;named&nbsp;fruit<br />//&nbsp;<br />//&nbsp;Notice:&nbsp;Use&nbsp;of&nbsp;undefined&nbsp;constant&nbsp;fruit&nbsp;-&nbsp;assumed&nbsp;'fruit'&nbsp;in...<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;apple<br /><br />//&nbsp;This&nbsp;defines&nbsp;a&nbsp;constant&nbsp;to&nbsp;demonstrate&nbsp;what's&nbsp;going&nbsp;on.&nbsp;&nbsp;The&nbsp;value&nbsp;'veggie'<br />//&nbsp;is&nbsp;assigned&nbsp;to&nbsp;a&nbsp;constant&nbsp;named&nbsp;fruit.<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'veggie'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Notice&nbsp;the&nbsp;difference&nbsp;now<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">];&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;apple<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">fruit</span><span style="color: #007700">];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;carrot<br /><br />//&nbsp;The&nbsp;following&nbsp;is&nbsp;okay,&nbsp;as&nbsp;it's&nbsp;inside&nbsp;a&nbsp;string.&nbsp;Constants&nbsp;are&nbsp;not&nbsp;looked&nbsp;for<br />//&nbsp;within&nbsp;strings,&nbsp;so&nbsp;no&nbsp;E_NOTICE&nbsp;occurs&nbsp;here<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;$arr[fruit]"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Hello&nbsp;apple<br /><br />//&nbsp;With&nbsp;one&nbsp;exception:&nbsp;braces&nbsp;surrounding&nbsp;arrays&nbsp;within&nbsp;strings&nbsp;allows&nbsp;constants<br />//&nbsp;to&nbsp;be&nbsp;interpreted<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;{$arr[fruit]}"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Hello&nbsp;carrot<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;{$arr['fruit']}"</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Hello&nbsp;apple<br /><br />//&nbsp;This&nbsp;will&nbsp;not&nbsp;work,&nbsp;and&nbsp;will&nbsp;result&nbsp;in&nbsp;a&nbsp;parse&nbsp;error,&nbsp;such&nbsp;as:<br />//&nbsp;Parse&nbsp;error:&nbsp;parse&nbsp;error,&nbsp;expecting&nbsp;T_STRING'&nbsp;or&nbsp;T_VARIABLE'&nbsp;or&nbsp;T_NUM_STRING'<br />//&nbsp;This&nbsp;of&nbsp;course&nbsp;applies&nbsp;to&nbsp;using&nbsp;superglobals&nbsp;in&nbsp;strings&nbsp;as&nbsp;well<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;$arr['fruit']"</span><span style="color: #007700">;<br />print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;$_GET['foo']"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Concatenation&nbsp;is&nbsp;another&nbsp;option<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;Hello&nbsp;apple<br /></span><span style="color: #0000BB">?&gt;</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&#039;s inside the square brackets (&#039;<i>[</i>&#039; and    &#039;<i>]</i>&#039;) 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">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</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">?&gt;</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 + -