Php list function usage

Source: Internet
Author: User
Tags numeric

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.