Home > Backend Development > PHP Tutorial > Usage of php list function_PHP tutorial

Usage of php list function_PHP tutorial

WBOY
Release: 2016-07-13 17:12:54
Original
814 people have browsed it

The role of the list function in PHP is to assign the values ​​​​in the array to some variables, so that we can use the list to implement simple data operations. Oh, you can refer to it if necessary.

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

Example 1. list() example

 代码如下 复制代码

$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()

 代码如下 复制代码


 
 

 
 
$result = mysql_query("SELECT id, name, salary FROM employees",$conn);
while (list($id, $name, $salary) = mysql_fetch_row($result)) {
    echo " n".
         "  n".
         "  n".
         " n";
}
 
?>
 
Employee nameSalary
$name$salary

Warning
list() assigns values ​​starting from the rightmost parameter. If you use simple variables, don't worry about this. But if you use an indexed array, usually you expect the result to be left to right as written in list(), but it's not. are assigned in reverse order.

Example 3. Using array index in list()

The code is as follows
 代码如下 复制代码
$info = array('coffee', 'brown', 'caffeine');
list($a[0], $a[1], $a[2]) = $info;
var_dump($a);
?>
Copy code

代码如下 复制代码

array(3) {
[2]=>
  string(8) "caffeine"
  [1]=>
  string(5) "brown"
  [0]=>
  string(6) "coffee"
}

$info = array('coffee', 'brown', 'caffeine'); list($a[0], $a[1], $a[2]) = $info; var_dump($a); ?> Produces the following output (note the comparison of the unit order and the order as 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" }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629273.htmlTechArticleThe function of list function in php is to assign the values ​​​​in the array to some variables, so that in simple data operations At this time, we can use list to achieve it. You can refer to it if necessary. Note: li...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template