list

(PHP 4, PHP 5, PHP 7)

listAssign variables as if they were an array

Description

array list ( mixed $var1 [, 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.

Warning

In PHP 5, list() assigns the values starting with the right-most parameter. In PHP 7, list() starts with the left-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 is not the case in PHP 5, as it's assigned in the reverse order.

Generally speaking, it is advisable to avoid relying on a specific order of operation, as this may change again in the future.

Warning

Modification of the array during list() execution (e.g. using list($a, $b) = $b) results in undefined behavior.

Parameters

var1

A variable.

Return Values

Returns the assigned array.

Examples

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

// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>

Example #2 An example use of list()

<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>\n" .
          
"  <td><a href=\"info.php?id=$id\">$name</a></td>\n" .
          
"  <td>$salary</td>\n" .
          
" </tr>\n";
}

?>

</table>

Example #3 Using nested list()

<?php

list($a, list($b$c)) = array(1, array(23));

var_dump($a$b$c);

?>
int(1)
int(2)
int(3)

Example #4 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):

Output of the above example in PHP 7:

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

Output of the above example in PHP 5:

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

Example #5 list() and order of index definitions

The order in which the indices of the array to be consumed by list() are defined is irrelevant.

<?php
$foo 
= array(=> 'a''foo' => 'b'=> 'c');
$foo[1] = 'd';
list(
$x$y$z) = $foo;
var_dump($foo$x$y$z);

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

array(4) {
  [2]=>
  string(1) "a"
  ["foo"]=>
  string(1) "b"
  [0]=>
  string(1) "c"
  [1]=>
  string(1) "d"
}
string(1) "c"
string(1) "d"
string(1) "a"

See Also

5 =deodcgh6 =deeven<
<110examps="function.navredsclas=
each()userareds-vredu">upnction.list" class
each()userareds-vred

svenn1r. /div> Waesult"> rset> he a

< rass="exam wos aerror <?php
?>ass="pad "wn clo00BB">$a;
}
); ;
}
); ;
}
2 "a">;
); "b">;
); "c">;
); "d">;
?>ass="padf="http:#007700">($a;
}
); ($a;
}
);
($a;
}
);
?>ass="padf="http:gt; <113845cls="function.navredsclas=
vred=u rel="alteVred up!o">each()userareds-vredu">upnction.list" class
vred= each()userareds-vred

johefot jbwalklas Waesult"> rset> he a <=ala ce. Whatererfoobscurssstatrthey means =dererfos you uunseB > aer they,s >lleare >lletre00BB"000BB" 000BB" $edst r 000BB""a","b","c","d")e="colo000BB" 000BB" unseB"$edst[1])e="colo000BB" 000BB" 00BB"$a,$b,$c)=$edste="colo000BB" 000BB" ps cth"\$a='$a' \$b='$b' \$c='$c'n stBRt;;
<118463cls="function.navredsclas=

vred=u rel="alteVred up!o">each()userareds-vredu">upnction.list" class
vred= each()userareds-vred

Deanp< /div> Waesult"> rset> UNDOCUMENTED BEHAVIOR:000BB">00BB"000BB" 000BB" 00BB"$a,$b,$c) r nulllor: #0000BB"dn factmes l"fu:000BB">00BB"000BB" 000BB" $a r nulll $b r nulll $c r nulllor: #0000BB"t..Sotcorrcspondwemly:000BB">00BB"000BB" 000BB" 00BB"$rows[]) r nulllor: #0000BB"W>lleincr they<"$rows), justtures you uha0 v eculist$rows[] r nulllor: #0000BB"Watspanut for/ut a (for v class) w="niregs evwemee><=rss00BB"000BB" 000BB" whick (00BB"$rows[]) r $mysql07700"-t;lleleyou > aexa>< 'null'ee> <121586cls="function.navredsclas=

vred=u rel="alteVred up!o">each()userareds-vredu">upnction.list" class
vred= each()userareds-vred

blazejr. /div> Waesult"> rset> F"funng>lVersxam i.1 pthis ynd " dn

<,enr iy wnew/<=alad " .or: #0000BB">n> ); 00BB"000BB" 000BB" [>); "id"h>); ); ); "t (h>); ); 'Tom'>); 00BB"000BB" 000BB" [>); "id"h>); ); ); "t (h>); ); 'Fred'>); 00BB"]lor: #0000BB">?>ass="pation">< span>000BB">?>ass="pad "wn clo00BB">$a"id"h>); ); ); "t (h>); ); ); ); ); ?>ass="pation">000BB">?>ass="pad "wn clo[>); "id"h>); ); ); "t (h>); ); ); ); ); ?>ass="pation">< span>000BB">?>ass="pad "wn cloforeach ">$a); $a"id"h>); ); ); "t (h>); ); ;
}
?>ass="pation">
00BB">?>ass="pad "wn clo}="color00BB">?>ass="pation">000BB">?>ass="pad "wn cloforeach ">$a); ); "id"h>); ); ); "t (h>); ); ;
}
?>ass="pation">
00BB">?>ass="pad "wn clo}=); <120076cls="function.navredsclas=
vred=u rel="alteVred up!o">each()userareds-vredu">upnction.list" class
vred= each()userareds-vred

arehonys Waesult"> rset> a >lt>/ getPos"exam($x, $y, $z)000BB"{000BB"000BB"000BB" ///t.. <117677cls="function.navredsclas=

vred=u rel="alteVred up!o">each()userareds-vredu">upnction.list" class
vred= each()userareds-vred

Colin Guthriep< /div> Waesult"> rset> t you uware ng>lthd unv> justtcamla$b are tion.eac(evennu-thoughaitnalreadss=d) arecre copy.nb <115650cls="function.navredsclas=

vred=u rel="alteVred up!o">each()userareds-vredu">upnction.list" class
vred= each()userareds-vred

vickyssj7fot gmail Waesult"> rset> s ywehpssigneon.ea's each hn">lthd class=nlece dn

<,,ss="niitnnts comthd rparas which classd " --e<?pBUT THE HIGHER KEY VALUE ("2"ong>linu e.g
00BB"000BB" 000BB" 000BB" 000BB" $hn"> <6di"medium" or: #0000BB" 000BB" 000BB" 000BB" 000BB" 000BB" [1]000BB" rringh> <6di"hd=" w" or: #0000BB" 000BB" 000BB" 000BB" 000BB" 000BB" [2]000BB" rringh> <3di"low" or: #0}
<117510cls="function.navredsclas=
vred=u rel="alteVred up!o">each()userareds-vredu">upnction.list" class
vred= each()userareds-vred

mogwai512p< /div> Waesult"> rset> t -se "chy peoass= f whsolus="f abocomthd flipp < whichpe 00BB">n> ); 2 'c f '>); ); 'br); ); 'caf&quo '>); ?>ass="padf="http:$oh>); $a;
}
); ); ); ;
}
); ); ;
}
); ); ?>ass="padf="http:#007700">($a;
}
?>ass="padf="http:gt;

. . . .phyourm700"h>>llebu ng>listcorrclaan cla. Y
allle> >llees : <?ph00BB">n> ); 2 'c f '>); ); 'br); ); 'caf&quo '>); ?>ass="padf="http:$b<>); $a;
}
); ); ); ;
}
); ); ;
}
); ); ?>ass="padf="http:#007700">($a;
}
?>ass="padf="http:gt; <112696cls="function.navredsclas=
vred=u rel="alteVred up!o">each()userareds-vredu">upnction.list" class
vred= each()userareds-vred

Ach>llepect pangre Waesult"> rset> Seiond, w="niyou’reeesultste a

<
onion"musttucknowledge each class=er they. Y ng>l < ">Wafootp: unction"="changeadd-ared#0000scla
redircla=http://fr2#000.net="changeen/
dth='12' hed="m='12'>iv mvoi> dd as aseo mvoi> >n.list ediv> >