PHP list function Usage _php tutorial

Source: Internet
Author: User
In PHP the role of the list function is to assign the values in the array to some variables, so that in the simple data manipulation we can use the list to achieve oh, there is a need to refer to.

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

Example 1. List () example

The code is as follows Copy Code

$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";

?>

Example 2. Example of using list ()

copy code


















!--? php $result = mysql_query ("Select ID, Name, salary from Employees", $conn) and while (list ($id, $name , $salary) = Mysql_fetch_row ($result)) {echo " n". " n". " n". " n";} ?>
Employee name Salary
$name $salary

Warning
List () assigns a value from the rightmost parameter. If you use a simple variable, don't worry about it. But if you use an indexed array, you usually expect the results to be as left-to-right as the one written in list (), but not actually. are assigned in the reverse order.

Example 3. Using the array index in list ()

The code is as follows Copy Code
$info = Array (' coffee ', ' brown ', ' caffeine ');
List ($a [0], $a [1], $a [2]) = $info;
Var_dump ($a);
?>

Produce the following output (note the comparison of the sequence of cells and the order written in the list () syntax):

The code is as follows Copy Code

Array (3) {
[2]=>
String (8) "Caffeine"
[1]=>
String (5) "Brown"
[0]=>
String (6) "Coffee"
}

http://www.bkjia.com/PHPjc/629273.html www.bkjia.com true http://www.bkjia.com/PHPjc/629273.html techarticle in PHP The role of the list function is to assign the values in the array to some variables, so that in the simple data manipulation we can use the list to achieve oh, there is a need to refer to. Note: Li ...

  • 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: info-contact@alibabacloud.com 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.