note 107208 deleted from function.list by googleguy

From: Date: Thu, 13 Sep 2012 09:59:05 +0000
Subject: note 107208 deleted from function.list by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190690@lists.php.net to get a copy of this message
Note Submitter: dan dot lugg at gmail dot com ---- Although the warning claims of undefined behaviour, it's reasonably straightforward what happens. <?php $a = array(1, 2, 3); list($a, $b, $c) = $a; var_dump($a, $b, $c); // int(1) // int(2) // int(3) ?> Since list() works from right to left on the target variables, $a is replaced last, thus keeping the remaining array element values around long enough to populate $b and $c. <?php $a = array(1, 2, 3); list($c, $b, $a) = $a; var_dump($a, $b, $c); // int(3) // NULL // NULL ?> $a is populated first (remember, right to left) with the value 3 from itself. Since $a is no longer an array, list() is forced to substitute NULL values for $b and $c. This appears to be perfectly sensible, though there may be edge cases and other strange behaviour I'm unaware of.

« previous php.notes (#190690) next »