php-doc-en/reference/array/functions/list.xml
Hannes Magnusson c030e2adf7 Upgrade to DocBook5:
- All id attributes are now xml:id
 - Add docbook namespace to all root elements
 - Replace <ulink /> with <link xlink:href />
 - Minor markup fixes here and there


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@238160 c90b9560-bf6c-de11-be94-00142212c4b1
2007-06-20 22:25:43 +00:00

154 lines
3.7 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.17 $ -->
<!-- splitted from ./en/functions/array.xml, last change in rev 1.2 -->
<refentry xml:id="function.list" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>list</refname>
<refpurpose>Assign variables as if they were an array</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>list</methodname>
<methodparam><type>mixed</type><parameter>varname</parameter></methodparam>
<methodparam rep="repeat"><type>mixed</type><parameter>...</parameter></methodparam>
</methodsynopsis>
<para>
Like <function>array</function>, this is not really a function,
but a language construct. <function>list</function> is used to
assign a list of variables in one operation.
</para>
<note>
<para>
<function>list</function> only works on numerical arrays and assumes
the numerical indices start at 0.
</para>
</note>
<para>
<example>
<title><function>list</function> examples</title>
<programlisting role="php">
<![CDATA[
<?php
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.\n";
// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!\n";
// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>An example use of <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)) {
echo " <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> assigns the values starting with the right-most
parameter. If you are using plain variables, you don'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
<function>list</function> from left to right; which it isn't. It's
assigned in the reverse order.
</para>
</warning>
<para>
<example>
<title>Using <function>list</function> with array indices</title>
<programlisting role="php">
<![CDATA[
<?php
$info = array('coffee', 'brown', 'caffeine');
list($a[0], $a[1], $a[2]) = $info;
var_dump($a);
?>
]]>
</programlisting>
<para>
Gives the following output (note the order of the elements compared in
which order they were written in the <function>list</function> syntax):
</para>
<screen>
<![CDATA[
array(3) {
[2]=>
string(8) "caffeine"
[1]=>
string(5) "brown"
[0]=>
string(6) "coffee"
}
]]>
</screen>
</example>
</para>
<para>
See also <function>each</function>, <function>array</function>
and <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
-->