Htaccess    html   Php    javascript   Asp   css    maths  Past Questions  Practice Tests Online

list function syntax tag tutorial 2013





Alert! Connect with 10,000 Chating Online. Join Now

Php list () function

Write your php code

 
<?php ?>

Php Result


Your code below



(PHP 3, PHP 4, PHP 5 )

list --  Assign variables as if they were an array

Syntax

void list ( mixed varname, mixed ... )

Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.

Note: list() only works on numerical arrays and assumes the numerical indices start at 0.

Example 1. list() examples

<?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. An example use of list()

<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() assigns the values starting with the right-most parameter. If you are using plain variables, you don't have to worry about this. But if you are using arrays with indices you usually expect the order of the indices in the array the same you wrote in the list() from left to right; which it isn't. It's assigned in the reverse order.

Example 3. Using list() with array indices

<?php

$info
= array('coffee', 'brown', 'caffeine');

list(
$a[0], $a[1], $a[2]) = $info;

var_dump($a);

?>

Gives the following output (note the order of the elements compared in which order they were written in the list() syntax):

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

See also each(), array() and extract().

Php list Function syntax tag

list php code on this is provided for your study purpose, it will guide you to know how create and design a website using php. use it to practice and train your self online



Php list syntax tutorial

php tutorial guide and code design are for easy learning and programming. The code practice section provided at the top is for practising of this syntax. Use the code section up to practice your php programming online. Learning php is very easy, all you need is to use the examples on this site and practice them to perfect your skills.


Function index

Variables

Expressions

Operators

Control-Structures

Cookies

Array

Date & Time

Directory function

File

Image

Operators

Form data handling

Mathematics operators

Mail

Php Mysql

Network Functions

Strings

php tutorial guides,functions, classes, code examples and tags for creating simple dynamic site to mysql database driven sites
 
 
Htaccess    html   Php    javascript   Asp   css    maths  Past Questions  Practice Tests Online