Php list function usage

Source: Internet
Author: User

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"
}

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.