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