Home >Backend Development >PHP Tutorial >PHP list() A simple example of assigning values in an array to variables, list array_PHP tutorial
list()
PHP list() assigns the values in the array to some variables in one step. Like array(), list() is not a true function, but a language construct.
Grammar:
void list( mixed var, mixed ... )Note: list() can only be used on numerically indexed arrays and assumes that numerical indexing starts from 0.
Example 1:
<?php $arr_age = array(18, 20, 25); list($wang, $li, $zhang) = $arr_age; echo $wang; //输出:18 echo $zhang; //输出:25 ?>
Example 2, data table query:
$result = mysql_query("SELECT id, username, email FROM user",$conn); while(list($id, $username, $email) = mysql_fetch_row($result)) { echo "用户名:$username<br />"; echo "电子邮箱:$email"; }
list() uses array index
List() allows the use of another array to receive the values assigned to the array, but when using an index array, the order of assignment is reversed from the order listed in list():
$arr_age = array(18, 20, 25);
list($a[0], $a[1], $a[2]) = $arr_age;
print_r($a); The output $a array structure is as follows:
Array ( [2] => 25 [1] => 20 [0] => 18 )
The above simple example of PHP list() assigning the value in the array to a variable is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Bangkejia more.