Home  >  Article  >  Backend Development  >  Example usage of list() function in php and how to assign values ​​in array to variables

Example usage of list() function in php and how to assign values ​​in array to variables

伊谢尔伦
伊谢尔伦Original
2018-05-15 09:51:591575browse

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.

Syntax:

void list( mixed var, mixed ... )

Note: list() can only be used on numerically indexed arrays and assumes that numerical indexing starts at 0.

The list() function in PHP is used to assign values ​​to a set of variables in one operation.

Like array(), this is not really a function, but a language construct. List() is used to specify one of the variables in the list Job.

Example:

Example 1:

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
"; echo "电子邮箱:$email"; }

list() using array index

list() allows the use of another array to receive the values ​​assigned by the array, but when using an index array, the order of assignment is opposite to 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 is the detailed content of Example usage of list() function in php and how to assign values ​​in array to variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn