|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #51112 list() misbehaviour
Submitted: 2010-02-22 12:38 UTC Modified: 2011-12-08 21:26 UTC
From: post at oliver-schieche dot de Assigned: nikic (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.2.12 OS: Debian (etch)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
24 + 14 = ?
Subscribe to this entry?

 [2010-02-22 12:38 UTC] post at oliver-schieche dot de
When assigning an array to a list() of variables and using the input array as a member of that list(), unexpected results are returned.

Reproduce code:
$b = array('foo','bar');
list($a,$b) = $b;

$c = array('foo','bar');
list($c,$d) = $c;

Expected result:
$a == $c == 'foo'
$b == $d == 'bar'

Actual result:
$a == 'b'
$b == 'bar'

$c == 'foo'
$d == 'bar'


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-22 12:42 UTC]
That's pretty much the same thing you can see with foreach, the array should not be altered while looping (the array itself, the data of a given element can be altered). No bug here, but should be documented.
 [2010-02-22 12:58 UTC] post at oliver-schieche dot de
Why document an obvious bug instead of fixing it? How would you document/explain this?

If this works:

    function ret($array) {return $array;}
    $b = array('foo','bar');
    list($a,$b) = ret($b);

But this doesn't:

    function &ret(&$array) {return $array;}
    $b = array('foo','bar');
    list($a,$b) = ret($b);

Then there's a referencing error hidden somewhere...
 [2010-03-18 18:38 UTC]
-Package: Documentation problem +Package: Scripting Engine problem
 [2010-03-18 18:38 UTC]
Can this either be classfied as a scripting engine bug. If it indeed is a documentation issue then re-classify this as a "To be Documented" documentation issue for the developer that picks this one up.
 [2010-03-18 18:43 UTC]
-Type: Documentation Problem +Type: Bug
 [2010-03-21 19:52 UTC]
-Status: Open +Status: To be documented
 [2010-03-21 19:52 UTC]
The order in which the variables are picked up in this case is "undefined behavior" - both variables are needed during the "list" operation.
 [2010-03-22 01:00 UTC] post at oliver-schieche dot de
Hate to be nagging again, but there's still a problem: it works "as it's supposed to" (read: "as it's not documented") in PHP4, but doesn't in PHP5 with the exception, that the following produces expected results:

$a = array('Foo','Bar');
list($a,$b) = $a;

$a == 'Foo'  ==>  TRUE
$b == 'Bar'  ==>  TRUE

"both variables are needed during the "list" operation." is a somewhat confusing statement regarding the issue(s) above.
 [2010-03-22 12:00 UTC]
Like Johannes said and Pierre said, writing to a variable you are iterating over 
gives undefined behaviour.  That means it will work in some cases and not in 
others and may very well change between versions.  There is nothing to fix here 
except perhaps a little note in the documentation.
 [2011-11-16 13:56 UTC]
-Package: Scripting Engine problem +Package: Documentation problem
 [2011-12-08 21:24 UTC]
-Type: Bug +Type: Documentation Problem
 [2011-12-08 21:25 UTC]
Automatic comment from SVN on behalf of nikic
Log: Fix Doc Bug #51112: list() misbehaviour

Add a warning saying that modification of the array being destructured during list() execution will result in undefined behavior.
 [2011-12-08 21:26 UTC]
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

 [2011-12-08 21:26 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jul 18 23:01:30 2024 UTC