List () function instance usage in PHP and how to assign values in an array to a variable

Source: Internet
Author: User
The PHP list () assigns the values in the array to some variables in one step. Like array (), list () is not a real function, but a language structure.


void list (mixed var, mixed ...)

Note: List () can only be used for arrays of numeric indexes and assumes that the numeric index starts at 0.

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

Like Array (), this is not a real feature, but a language structure. List () is used to specify one of the variables in the list of jobs.


<?php$info = Array (' coffee ', ' brown ', ' caffeine ');//Listing all the Variableslist ($drink, $color, $power) = $info; echo "$drink is $color and $power makes it SPECIAL.N";//Listing Some of themlist ($drink,, $power) = $info; echo "$drink has $ POWER.N ";//Or Let's skip to only the third onelist (,, $power) = $info; echo" I need $power!n ";//list () doesn ' t work wit H stringslist ($bar) = "ABCDE"; Var_dump ($bar); Null?>

Example 1:

<?php$arr_age = Array ($wang, $li, $zhang) = $arr _age;echo $wang;    Output: 18echo $zhang;    Output:25?>

Example 2, data table query:

$result = mysql_query ("SELECT ID, username, email from user", $conn) and while (list ($id, $username, $email) = Mysql_fetch_row ( $result) {  echo "user name: $username <br/>";  echo "e-mail: $email";}

List () using array index

The list () allows the use of another array to receive values from the array, except that when using an indexed array, the order of assignment is the opposite of the order listed in list ():

$arr _age = Array (1, 2), list ($a [0], $a, $a [+]) = $arr _age;

Print_r ($a); the output $a array structure is as follows:

Array ([2] = [1] = [0] = 18)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.