list ASSIGN variables as if they were an ARRAY.
This is not a really function, but a language contruct that is used to assign a list of variables in one operation. Before PHP 7.1.0, list only worked on numerical ARRAYS and assumes the numerical indices start at 0. Since PHP 7.0.0, list starts with the left-most parameter, in earlier versions with the rightmost 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. In the future, this may change, so it's best not to rely on a specific order.
<?php arr list ( mix $var1 [, mix $var2 , ..., mix $varN ] ) where , $var1 = A variable to assign $var2 = The second variable to assign . . . . . . . . . $varN = The last variable to assign ?>
$var1
The FIRST variable to assign.
$var2
The SECOND variable to assign.
$varN
The LAST variable to assign.
EXERCISE
<?php $record = [ "Pink Floyd" , "Louder than Words" , "The Endless River" , "Classic rock" , "2004" , "Sony Music" ]; list ( $band , $music , $album , $musictype , $year , $recordcompany ) = $record ; echo 'Music: ' . $music . '<br>Band: ' . $band . '<br>Year: ' . $year . '<br>Album: ' . $album . '<br>Record Company: ' . $recordcompany . '<br>Music Type: ' . $musictype ; ?>
RESULT Music: Louder than Words
Band: Pink Floyd
Year: 2014
Album: The Endless River
Record Company: Sony Music
Music Type: Classic rock
EXERCISE
<?php $locality = [ "São Paulo" , "Praça de Sé" , "-23.5503099" , "-46.6342009" , "01001-000" ]; list ( $state , $address , $lati , $long , $zipcode ) = $locality ; echo 'Locality: ' . $address . '<br>State: ' . $state . '<br>Position = (' . $lati . ', ' . $long . ') <br>Zip Code: ' . $zipcode ; ?>
RESULT Locality: Praça da Sé
State: São Paulo
Year: 2014
Position = (-23.5503099, -46.6342009)
Zip Code: 01001-000
EXERCISE
<?php foreach(array(array( 1 , 2 ), array( 3 , 4 )) as list( $a , $b )) { echo '- - - - - - - - - - - - -<pre>' ; var_dump ( $a . $b ); echo '</pre>' ; } $array = array( array( 'a' , 'b' ), array( 'c' , 'd' ), ); foreach ( $array as list( $a , $b )) { echo '- - - - - - - - - - - - -<pre>' ; var_dump ( $a . $b ); echo '</pre>' ; } $multi = array( array(array( 1 , 2 ), array( 3 , 4 )), array(array( 5 , 6 ), array( 7 , 8 )), ); foreach ( $multi as list(list( $a , $b ), list( $c , $d ))) { echo '- - - - - - - - - - - - -<pre>' ; var_dump ( $a . $b . $c . $d ); echo '</pre>' ; } foreach ( $multi as $key => list(list( $a , $b ), list( $c , $d ))) { echo '- - - - - - - - - - - - -<pre>' ; var_dump ( $key . $a . $b . $c . $d ); echo '</pre>' ; } ?>
EXERCISE
<?php foreach (array(array( 1 , 2 ), array( 3 , 4 )) as list( $a , )) { echo '- - - - - - - - - - - - -<pre>' ; var_dump ( $a ); echo '</pre>' ; } $array = [[ 'a' , 'b' ], 'c' , 'd' ]; foreach( $array as list(, $a )) { echo '- - - - - - - - - - - - -<pre>' ; var_dump ( $a ); echo '</pre>' ; } ?>
EXERCISE
<?php $array = [[ 'a' , 'b' ], 'c' , 'd' ]; foreach( $array as list( $key ) => list(list(), $a )) { } ?>
EXERCISE
<?php $array = [[ 'a' , 'b' ], 'c' , 'd' ]; foreach( $array as $key => list()) { } ?>
EXERCISE
<?php $points = [ [ "x" => 1 , "y" => 2 ], [ "x" => 2 , "y" => 1 ] ]; echo 'The given $points:<br>' ; var_dump ( $points ); foreach ( $points as list( "x" => $x , "y" => $y )) { echo '<br><br>' ; var_dump ( $x , $y ); } echo PHP_EOL ; $invertedPoints = [ "x" => [ 1 , 2 ], "y" => [ 2 , 1 ] ]; echo '<br><br>The given $invertedPoints:<br>' ; var_dump ( $invertedPoints ); foreach ( $invertedPoints as list( 0 => $row1 , 1 => $row2 )) { echo '<br><br>' ; var_dump ( $row1 , $row2 ); } ?>
EXERCISE
<?php list( $a ) = NULL ; list( $b ) = 1 ; list( $c ) = 1. ; list( $d ) = 'foo' ; list( $e ) = print '' ; echo '<pre>' ; var_dump ( $a , $b , $c , $d , $e ); echo '</pre>' ; ?>
EXERCISE
<?php $arr = array( 2 , 1 ); $b =& $arr ; list(, $a ) = $b ; echo '<pre>' ; var_dump ( $a , $b ); echo '</pre>' ; ?>
EXERCISE
<?php $str = "foo" ; list( $a , $b , $c ) = $str ; var_dump ( $a , $b , $c ); print "<br><br>" ; $int = 1 ; list( $a , $b , $c ) = $int ; var_dump ( $a , $b , $c ); print "<br><br>" ; $obj = new stdClass ; list( $a , $b , $c ) = $obj ; var_dump ( $a , $b , $c ); print "<br><br>" ; $arr = array( 1 , 2 , 3 ); list( $a , $b , $c ) = $arr ; var_dump ( $a , $b , $c ); ?>
EXERCISE
<?php list( $a , list( $b , list(list( $d )))) = array(); ?>
EXERCISE
<?php list( $x , $y ) = function() { }; var_dump ( $x , $y ); ?>
EXERCISE
<?php list([ $a ]) = [[ 1 ]]; var_dump ( $a ); ?>
EXERCISE
<?php var_dump ( list( 1 , 2 , 3 ) ); ?>
EXERCISE
<?php list(,,,,,,,,,,) = []; ?>
EXERCISE
<?php $antonyms = [ "good" => "bad" , "happy" => "sad" , ]; list( "good" => $good_antonym , "happy" => $happy_antonym ) = $antonyms ; var_dump ( $good_antonym , $happy_antonym ); echo PHP_EOL ; $powersOfTwo = [ 1 => 2 , 2 => 4 , 3 => 8 ]; list( 1 => $two_1 , 2 => $two_2 , 3 => $two_3 ) = $powersOfTwo ; var_dump ( $two_1 , $two_2 , $two_3 ); echo PHP_EOL ; $contrivedMixedKeyTypesExample = [ 7 => "the best PHP version" , "elePHPant" => "the cutest mascot" ]; list( 7 => $seven , "elePHPant" => $elePHPant ) = $contrivedMixedKeyTypesExample ; var_dump ( $seven , $elePHPant ); echo '<br><br>' ; echo PHP_EOL ; $allTogetherNow = [ "antonyms" => $antonyms , "powersOfTwo" => $powersOfTwo , "contrivedMixedKeyTypesExample" => $contrivedMixedKeyTypesExample ]; list( "antonyms" => list( "good" => $good_antonym , "happy" => $happy_antonym ), "powersOfTwo" => list( 1 => $two_1 , 2 => $two_2 , 3 => $two_3 ), "contrivedMixedKeyTypesExample" => list( 7 => $seven , "elePHPant" => $elePHPant ) ) = $allTogetherNow ; var_dump ( $good_antonym , $happy_antonym ); echo '<br><br>' ; var_dump ( $two_1 , $two_2 , $two_3 ); echo '<br><br>' ; var_dump ( $seven , $elePHPant ); ?>
EXERCISE
<?php $results = [ 0 => 0 , 1 => 1 , "" => "" ]; list( NULL => $NULL , 1.5 => $float , FALSE => $FALSE , TRUE => $TRUE ) = $results ; var_dump ( $NULL , $float , $FALSE , $TRUE ); echo '<br>' ; echo PHP_EOL ; list( "0" => $zeroString , "1" => $oneString ) = $results ; echo '<br>' ; var_dump ( $zeroString , $oneString ); echo '<br><br>' ; list( STDIN => $resource ) = []; ?>
EXERCISE
<?php // All the following should print 'a' then 'b' list( $a , $b ) = [ 'a' , 'b' ]; var_dump ( $a ); echo '<br>' ; var_dump ( $b ); echo '<br><br>' ; list( 0 => $a , 1 => $b ) = [ 'a' , 'b' ]; var_dump ( $a ); echo '<br>' ; var_dump ( $b ); echo '<br><br>' ; list( 1 => $b , 0 => $a ) = [ 'a' , 'b' ]; var_dump ( $a ); echo '<br>' ; var_dump ( $b ); echo '<br><br>' ; $arr = []; list( $arr [], $arr []) = [ 'a' , 'b' ]; var_dump ( $arr [ 0 ]); echo '<br>' ; var_dump ( $arr [ 1 ]); echo '<br><br>' ; $arr = []; list( 0 => $arr [], 1 => $arr []) = [ 'a' , 'b' ]; var_dump ( $arr [ 0 ]); echo '<br>' ; var_dump ( $arr [ 1 ]); echo '<br><br>' ; $arr = []; list( 1 => $arr [], 0 => $arr []) = [ 'b' , 'a' ]; var_dump ( $arr [ 0 ]); echo '<br>' ; var_dump ( $arr [ 1 ]); echo '<br><br>' ; $arr = []; list(list( 1 => $arr [], 0 => $arr [])) = [[ 'b' , 'a' ]]; var_dump ( $arr [ 0 ]); echo '<br>' ; var_dump ( $arr [ 1 ]); echo '<br><br>' ; $arr = []; list( 'key1' => $arr [], 'key2' => $arr []) = [ 'key2' => 'b' , 'key1' => 'a' ]; var_dump ( $arr [ 0 ]); echo '<br>' ; var_dump ( $arr [ 1 ]); echo '<br><br>' ; // This should print 'foo' $a = 0 ; list( $a => $a ) = [ 'foo' , 'bar' ]; var_dump ( $a ); echo '<br><br>' ; // This should print 'bar' then 'foo' $a = 0 ; $b = 1 ; list( $b => $a , $a => $c ) = [ 'bar' => 'foo' , 1 => 'bar' ]; var_dump ( $a ); echo '<br>' ; var_dump ( $c ); ?>
EXERCISE
<?php $i = 0 ; $a = [ 0 => [ 'b' => 'bar' , 'a' => 'foo' , ], 1 => 'a' , 3 => 'b' , ]; list( $a [ $i ++] => $a [ $i ++], $a [ $i ++] => $a [ $i ++]) = $a [ $i ++]; var_dump ( $i ); echo '<br>' ; // should be 5 var_dump ( $a [ 2 ]); echo '<br>' ; // should be 'foo' var_dump ( $a [ 4 ]); // should be 'bar' ?>
EXERCISE
<?php $arr = [ 1 => "one" , 2 => "two" , 3 => "three" ]; const COMPILE_TIME_RESOLVABLE = 1 ; define ( 'PROBABLY_NOT_COMPILE_TIME_RESOLVABLE' , file_get_contents ( "data:text/plain,2" )); $probablyNotCompileTimeResolvable3 = 3 * 1 ; list( COMPILE_TIME_RESOLVABLE => $one , PROBABLY_NOT_COMPILE_TIME_RESOLVABLE => $two , $probablyNotCompileTimeResolvable3 => $three ) = $arr ; echo '<pre>' ; var_dump ( $one , $two , $three ); echo '</pre>' ; ?>
EXERCISE
<?php $antonyms = [ "good" => "bad" , "happy" => "sad" , ]; list( "good" => $good , "happy" => $happy ) = $antonyms ; echo '<pre>' ; var_dump ( $good , $happy ); echo '</pre>' ; echo PHP_EOL ; $antonyms = [ "good" => "bad" , "happy" => "sad" , ]; list( "good" => $good , "happy" => $happy , ) = $antonyms ; echo '<pre>' ; var_dump ( $good , $happy ); echo '</pre>' ; ?>
EXERCISE
<?php $contrivedMixedKeyTypesExample = [ 7 => "the best PHP version" , "elePHPant" => "the cutest mascot" ]; list( 5 => $five , "duke" => $duke ) = $contrivedMixedKeyTypesExample ; var_dump ( $five , $duke ); ?>
EXERCISE
<?php $contrivedKeyedAndUnkeyedArrayExample = [ 0 , 1 => 1 , "foo" => "bar" ]; list( $zero , 1 => $one , "foo" => $foo ) = $contrivedKeyedAndUnkeyedArrayExample ; ?>
EXERCISE
<?php $points = [ [ "x" => 1 , "y" => 2 ], [ "x" => 2 , "y" => 1 ] ]; list(list( "x" => $x1 , "y" => $y1 ), list( "x" => $x2 , "y" => $y2 )) = $points ; echo '<pre>' ; var_dump ( $x1 , $y1 , $x2 , $y2 ); echo '</pre>' ; echo PHP_EOL ; $invertedPoints = [ "x" => [ 1 , 2 ], "y" => [ 2 , 1 ] ]; list( "x" => list( $x1 , $x2 ), "y" => list( $y1 , $y2 )) = $invertedPoints ; echo '<pre>' ; var_dump ( $x1 , $y1 , $x2 , $y2 ); echo '</pre>' ; ?>
EXERCISE
<?php $arr = array( 1 , array( 2 )); list(& $a , list(& $b )) = $arr ; var_dump ( $a , $b ); echo '<br><br>' ; var_dump ( $arr ); echo '<br><br><br><br>' ; $arr = array( 1 , array( 2 )); list( $a , & $b ) = $arr ; var_dump ( $arr ); echo '<br><br><br><br>' ; $arr = array( 1 , array( 2 )); [& $a , [& $b ]] = $arr ; var_dump ( $a , $b ); echo '<br><br>' ; var_dump ( $arr ); echo '<br><br><br><br>' ; $arr = array( 1 , array( 2 )); [& $a , [& $b ], & $c ] = $arr ; var_dump ( $a , $b , $c ); echo '<br><br>' ; var_dump ( $arr ); echo '<br><br><br><br>' ; $arr = array( "one" => 1 , "two" => array( 2 )); [ "one" => & $a , "two" => [& $b ], "three" => & $c ] = $arr ; var_dump ( $a , $b , $c ); echo '<br><br>' ; var_dump ( $arr ); ?>
EXERCISE
<?php $arr = array(new stdclass ); list(& $a , & $b ) = $arr ; var_dump ( $a , $b ); echo '<br><br>' ; var_dump ( $arr ); ?>
EXERCISE
<?php $a = 1 ; $b =& $a ; $arr = [& $a , & $b ]; list(& $a , & $b ) = $arr ; var_dump ( $a , $b , $arr ); echo '<br><br>' ; $b ++; var_dump ( $a , $b , $arr ); echo '<br><br>' ; unset( $a , $b , $arr ); /* * $a is first set as a reference to the 0'th elem, '1' * $a is then set to the value of the 1'st elem, '2' * $arr would look like, [2,2] * Increment $a, and it should be [3, 2] */ $arr = [ 1 , 2 ]; list(& $a , $a ) = $arr ; var_dump ( $a ); echo '<br><br>' ; $a ++; var_dump ( $arr ); echo '<br><br>' ; unset( $a , $arr ); /* * We do not allow references to the same variable of rhs. */ $a = [ 1 , 2 ]; $ref =& $a ; list(& $a , & $b ) = $a ; var_dump ( $a , $b ); echo '<br><br>' ; $a ++; $b ++; var_dump ( $ref ); ?>
EXERCISE
<?php list(& $foo ) = [ 42 ]; ?>
EXERCISE
<?php $a = [ 1 , 2 , 3 ]; list( $a , $b , $c ) = $a ; var_dump ( $a , $b , $c ); echo '<br><br>' ; $b = [ 1 , 2 , 3 ]; list( $a , $b , $c ) = $b ; var_dump ( $a , $b , $c ); echo '<br><br>' ; $c = [ 1 , 2 , 3 ]; list( $a , $b , $c ) = $c ; var_dump ( $a , $b , $c ); echo '<br><br>' ; $a = [[ 1 , 2 ], 3 ]; list(list( $a , $b ), $c ) = $a ; var_dump ( $a , $b , $c ); echo '<br><br>' ; $b = [[ 1 , 2 ], 3 ]; list(list( $a , $b ), $c ) = $b ; var_dump ( $a , $b , $c ); echo '<br><br>' ; $b = [ 1 , [ 2 , 3 ]]; list( $a , list( $b , $c )) = $b ; var_dump ( $a , $b , $c ); echo '<br><br>' ; $c = [ 1 , [ 2 , 3 ]]; list( $a , list( $b , $c )) = $c ; var_dump ( $a , $b , $c ); ?>