php.net |  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
 [2010-02-22 12:38 UTC] post at oliver-schieche dot de
Description:
------------
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'

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-22 12:42 UTC] [email protected]
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] [email protected]
-Package: Documentation problem +Package: Scripting Engine problem
 [2010-03-18 18:38 UTC] [email protected]
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] [email protected]
-Type: Documentation Problem +Type: Bug
 [2010-03-21 19:52 UTC] [email protected]
-Status: Open +Status: To be documented
 [2010-03-21 19:52 UTC] [email protected]
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] [email protected]
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] [email protected]
-Package: Scripting Engine problem +Package: Documentation problem
 [2011-12-08 21:24 UTC] [email protected]
-Type: Bug +Type: Documentation Problem
 [2011-12-08 21:25 UTC] [email protected]
Automatic comment from SVN on behalf of nikic
Revision: http://svn.php.net/viewvc/?view=revision&revision=320698
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] [email protected]
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] [email protected]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Dec 30 16:00:01 2025 UTC