mirror of
https://github.com/php/doc-zh.git
synced 2026-04-24 16:48:17 +02:00
64071f2fba
git-svn-id: https://svn.php.net/repository/phpdoc/zh/trunk@95699 c90b9560-bf6c-de11-be94-00142212c4b1
140 lines
3.3 KiB
XML
Executable File
140 lines
3.3 KiB
XML
Executable File
<?xml version="1.0" encoding="gb2312"?>
|
||
<!-- $Revision: 1.2 $ -->
|
||
<!-- $Author: dallas $ -->
|
||
<!-- EN-Revision: 1.6 Maintainer: dallas Status: ready -->
|
||
<refentry id="function.list">
|
||
<refnamediv>
|
||
<refname>list</refname>
|
||
<refpurpose>
|
||
把数组中的值赋给一些变量
|
||
</refpurpose>
|
||
</refnamediv>
|
||
<refsect1>
|
||
<title>说明</title>
|
||
<methodsynopsis>
|
||
<type>void</type><methodname>list</methodname>
|
||
<methodparam rep="repeat"><type>mixed</type><parameter>...</parameter></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
像 <function>array</function> 一样,这不是真正的函数,而是语言结构。<function>list</function>
|
||
用一步操作给一组变量进行赋值。
|
||
</para>
|
||
<note>
|
||
<para>
|
||
<function>list</function> 仅能用于数字索引的数组并假定数字索引从 0 开始。
|
||
</para>
|
||
</note>
|
||
<para>
|
||
<example>
|
||
<title><function>list</function> 例子</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
$info = array('coffee', 'brown', 'caffeine');
|
||
|
||
// Listing all the variables
|
||
list($drink, $color, $power) = $info;
|
||
print "$drink is $color and $power makes it special.\n";
|
||
|
||
// Listing some of them
|
||
list($drink, , $power) = $info;
|
||
print "$drink has $power.\n";
|
||
|
||
// Or let's skip to only the third one
|
||
list( , , $power) = $info;
|
||
print "I need $power!\n";
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>使用 <function>list</function> 的例子</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<table>
|
||
<tr>
|
||
<th>Employee name</th>
|
||
<th>Salary</th>
|
||
</tr>
|
||
|
||
<?php
|
||
|
||
$result = mysql_query ("SELECT id, name, salary FROM employees",$conn);
|
||
while (list ($id, $name, $salary) = mysql_fetch_row ($result)) {
|
||
print (" <tr>\n".
|
||
" <td><a href=\"info.php?id=$id\">$name</a></td>\n".
|
||
" <td>$salary</td>\n".
|
||
" </tr>\n");
|
||
}
|
||
|
||
?>
|
||
|
||
</table>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<warning>
|
||
<para>
|
||
<function>list</function> 从最右边一个参数开始赋值。如果你用单纯的变量,不用担心这一点。但是如果你用了具有索引的数组,通常你期望得到的结果和在
|
||
<function>list</function> 中写的一样是从左到右的,但实际上不是。是以相反顺序赋值的。
|
||
</para>
|
||
</warning>
|
||
<para>
|
||
<example>
|
||
<title>在 <function>list</function> 中使用数组索引</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
$info = array('coffee', 'brown', 'caffeine');
|
||
|
||
list($a[0], $a[1], $a[2]) = $info;
|
||
|
||
var_dump($a);
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
产生如下输出(注意单元顺序和 <function>list</function> 语法中所写的顺序的比较):
|
||
<screen>
|
||
array(3) {
|
||
[2]=>
|
||
string(8) "caffeine"
|
||
[1]=>
|
||
string(5) "brown"
|
||
[0]=>
|
||
string(6) "coffee"
|
||
}
|
||
</screen>
|
||
</para>
|
||
<para>
|
||
参见 <function>each</function>,<function>array</function> 和 <function>extract</function>。
|
||
</para>
|
||
</refsect1>
|
||
</refentry>
|
||
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"../../../../manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|