In php, the list function is used to assign values in the array to some variables. In this way, we can use list to implement simple data operations. For more information, see.
Note: list () can only be used as an array of numeric indexes. It is assumed that the numeric index starts from 0.
Example 1. list () Example
| The Code is as follows: |
Copy code |
<? Php
$ 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 ()
| The Code is as follows: |
Copy code |
<Table>
<Tr>
<Th> Employee name </th>
<Th> Salary </th>
</Tr>
<? Php
$ Result = mysql_query ("SELECT id, name, salary FROM employees", $ conn );
While (list ($ id, $ name, $ salary) = mysql_fetch_row ($ result )){
Echo "<tr> n ".
"<Td> <a href =" info. php? Id = $ id "> $ name </a> </td> n ".
"<Td> $ salary </td> n ".
"</Tr> n ";
}
?>
</Table>
|
Warning
List () is assigned a value starting from the rightmost parameter. If you use simple variables, don't worry about this. However, if you use an array with indexes, the expected results are usually the same as those written in list (), but they are not. Values are assigned in reverse order.
Example 3. Use array indexes in list ()
| The Code is as follows: |
Copy code |
<? Php
$ Info = array ('coffee ', 'Brown', 'caffeine ');
List ($ a [0], $ a [1], $ a [2]) = $ info;
Var_dump ($ );
?>
|
Generate the following output (compare the unit order with 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"
} |