php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52321 list() fails under a mere condition
Submitted: 2010-07-12 20:52 UTC Modified: 2010-07-12 21:02 UTC
From: saltybaldyev at gmx dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.3.2 OS: linux,windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: saltybaldyev at gmx dot com
New email:
PHP Version: OS:

 

 [2010-07-12 20:52 UTC] saltybaldyev at gmx dot com
Description:
------------
let $b=array(# , #).
then

list($a,$b) = $b

fails.

moar precise: after the assigment we'd get

$b = $b[1],
$a = {
  $b[1][0], if $b[1] is a string/array
  NULL, if $b[1] is a number
}

if $b[1] is an object we'd get a fatal error 'cant convert object->array'.


//////////////////

it works correctly if we try list($b,$a)=$b. so an easy fix could be forged in just 2 additional steps: swap arguments * do the usual work * swap results

Test script:
---------------
$a = 1;
$b = array('ohno','yeah');
list($a,$b) = $b;
print_r(array('a'=>$a,'b'=>$b));

Expected result:
----------------
Array
(
    [a] => ohno
    [b] => yeah
)


Actual result:
--------------
Array
(
    [a] => y
    [b] => yeah
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-12 20:58 UTC] saltybaldyev at gmx dot com
the same if we do list($a,$b,$c)=$c, list($a,$c,$b)=$c and so on.

i guess such assignments only work properly in case its right part goes to list() first: list(--> $c <--,$a1,$a2,$a3,...)=$c
 [2010-07-12 21:02 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-07-12 21:02 UTC] rasmus@php.net
You are overwriting the variable you are iterating over.  Just like with a foreach 
loop, there is no defined behaviour for that.  In this case it is assigned things 
right to left so $b gets set to "yeah" first and then $a gets set to the first 
array element of "yeah" which, since it is a string, is 'y'.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 13:01:36 2024 UTC