note 107208 added to function.list
| From: | dan dot lugg at gmail dot com | Date: | Sun, 15 Jan 2012 01:43:49 +0000 |
| Subject: | note 107208 added to function.list | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185665@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 216.235.13.26
Probable Submitter: 216.121.235.92
----
Manual Page -- http://www.php.net/manual/en/function.list.php
Edit -- https://master.php.net/note/edit/107208
Del: integrated -- https://master.php.net/note/delete/107208/integrated
Del: useless -- https://master.php.net/note/delete/107208/useless
Del: bad code -- https://master.php.net/note/delete/107208/bad+code
Del: spam -- https://master.php.net/note/delete/107208/spam
Del: non-english -- https://master.php.net/note/delete/107208/non-english
Del: in docs -- https://master.php.net/note/delete/107208/in+docs
Del: other reasons-- https://master.php.net/note/delete/107208
Reject -- https://master.php.net/note/reject/107208
Search -- https://master.php.net/manage/user-notes.php