function.list.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 175 行

HTML
175
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Assign variables as if they were an array</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ksort.html">ksort</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.natcasesort.html">natcasesort</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.list" class="refentry"> <div class="refnamediv">  <h1 class="refname">list</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">list</span> &mdash; <span class="dc-title">Assign variables as if they were an array</span></p> </div> <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type"><span class="type void">void</span></span> <span class="methodname"><b><b>list</b></b></span>    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$varname</tt></span>   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$...</tt></span>  ] )</div>  <p class="para rdfs-comment">   Like <a href="function.array.html" class="function">array()</a>, this is not really a function,   but a language construct.  <b>list()</b> is used to   assign a list of variables in one operation.  </p> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">varname</tt></i></span>     <dd>      <p class="para">       A variable.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   No value is returned.  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 <b>list()</b> examples</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$info&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'coffee'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'brown'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'caffeine'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Listing&nbsp;all&nbsp;the&nbsp;variables<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$drink</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$power</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"$drink&nbsp;is&nbsp;$color&nbsp;and&nbsp;$power&nbsp;makes&nbsp;it&nbsp;special.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Listing&nbsp;some&nbsp;of&nbsp;them<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$drink</span><span style="color: #007700">,&nbsp;,&nbsp;</span><span style="color: #0000BB">$power</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"$drink&nbsp;has&nbsp;$power.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Or&nbsp;let's&nbsp;skip&nbsp;to&nbsp;only&nbsp;the&nbsp;third&nbsp;one<br /></span><span style="color: #007700">list(&nbsp;,&nbsp;,&nbsp;</span><span style="color: #0000BB">$power</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;need&nbsp;$power!\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;list()&nbsp;doesn't&nbsp;work&nbsp;with&nbsp;strings<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;NULL<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #2 An example use of <b>list()</b></b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000">&lt;table&gt;<br />&nbsp;&lt;tr&gt;<br />&nbsp;&nbsp;&lt;th&gt;Employee&nbsp;name&lt;/th&gt;<br />&nbsp;&nbsp;&lt;th&gt;Salary&lt;/th&gt;<br />&nbsp;&lt;/tr&gt;<br /><br /><span style="color: #0000BB">&lt;?php<br /><br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;id,&nbsp;name,&nbsp;salary&nbsp;FROM&nbsp;employees"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br />while&nbsp;(list(</span><span style="color: #0000BB">$id</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$salary</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">mysql_fetch_row</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;tr&gt;\n"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;&lt;td&gt;&lt;a&nbsp;href=\"info.php?id=$id\"&gt;$name&lt;/a&gt;&lt;/td&gt;\n"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;&lt;td&gt;$salary&lt;/td&gt;\n"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;/tr&gt;\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;<br /></span><br />&lt;/table&gt;</span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #3 Using nested <b>list()</b></b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;list(</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">))&nbsp;=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><pre><div class="cdata"><pre>int(1)int(2)int(3)</pre></div>    </pre></div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #4 Using <b>list()</b> with array indices</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$info&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'coffee'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'brown'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'caffeine'</span><span style="color: #007700">);<br /><br />list(</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">])&nbsp;=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>     Gives the following output (note the order of the elements compared in     which order they were written in the <b>list()</b> syntax):    </p></div>    <div class="example-contents"><pre><div class="cdata"><pre>array(3) {  [2]=&gt;  string(8) &quot;caffeine&quot;  [1]=&gt;  string(5) &quot;brown&quot;  [0]=&gt;  string(6) &quot;coffee&quot;}</pre></div>    </pre></div>   </div>  </p> </div> <div class="refsect1 notes">  <h3 class="title">Notes</h3>  <div class="warning"><b class="warning">Warning</b>   <p class="para">    <b>list()</b> assigns the values starting with the right-most    parameter. If you are using plain variables, you don&#039;t have to worry    about this. But if you are using arrays with indices you usually expect    the order of the indices in the array the same you wrote in the    <b>list()</b> from left to right; which it isn&#039;t. It&#039;s    assigned in the reverse order.   </p>  </div>  <blockquote><p><b class="note">Note</b>:        <b>list()</b> only works on numerical arrays and assumes    the numerical indices start at 0.   <br />  </p></blockquote> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.each.html" class="function" rel="rdfs-seeAlso">each()</a></li>    <li class="member"><a href="function.array.html" class="function" rel="rdfs-seeAlso">array()</a></li>    <li class="member"><a href="function.extract.html" class="function" rel="rdfs-seeAlso">extract()</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.ksort.html">ksort</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.natcasesort.html">natcasesort</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

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