Query: list
OS: php
Section: 3
Format: Original Unix Latex Style Formatted with HTML and a Horizontal Scroll Bar
LIST(3) 1 LIST(3) list - Assign variables as if they were an arraySYNOPSISarray list (mixed $var1, [mixed $...])DESCRIPTIONLike array(3), this is not really a function, but a language construct. list(3) is used to assign a list of variables in one operation.PARAMETERSo $var1 - A variable.RETURN VALUESReturns the assigned array.EXAMPLESExample #1 list(3) 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. "; // Listing some of them list($drink, , $power) = $info; echo "$drink has $power. "; // Or let's skip to only the third one list( , , $power) = $info; echo "I need $power! "; // list() doesn't work with strings list($bar) = "abcde"; var_dump($bar); // NULL ?> Example #2 An example use of list(3) <table> <tr> <th>Employee name</th> <th>Salary</th> </tr> <?php $result = $pdo->query("SELECT id, name, salary FROM employees"); while (list($id, $name, $salary) = $result->fetch(PDO::FETCH_NUM)) { echo " <tr> " . " <td><a href="info.php?id=$id">$name</a></td> " . " <td>$salary</td> " . " </tr> "; } ?> </table> Example #3 Using nested list(3) <?php list($a, list($b, $c)) = array(1, array(2, 3)); var_dump($a, $b, $c); ?> int(1) int(2) int(3) Example #4 Using list(3) 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(3) syntax): array(3) { [2]=> string(8) "caffeine" [1]=> string(5) "brown" [0]=> string(6) "coffee" }NOTESWarning list(3) 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(3) from left to right; which it isn't. It's assigned in the reverse order. Warning Modification of the array during list(3) execution (e.g. using list($a, $b) = $b) results in undefined behavior. Note list(3) only works on numerical arrays and assumes the numerical indices start at 0.SEE ALSOeach(3), array(3), extract(3). PHP Documentation Group LIST(3)
Related Man Pages |
---|
isset(3) - php |
splfixedarray(3) - php |
each(3) - php |
explode(3) - php |
mongocursor.info(3) - php |
Similar Topics in the Unix Linux Community |
---|
difference between echo and "" |
How do you like your coffee? |
How to echo space or tab delimited values into rows? |
between command? |
Problems passing strings within an array |