Skip to content

list() Unpacking in foreach Declaration #60

@WyrdNexus

Description

@WyrdNexus

list() Unpacking in foreach Declaration Affects Syntax Error

PHP Feature added in 5.5, so likely simply hasn't been reviewed yet.
http://php.net/manual/en/control-structures.foreach.php#control-structures.foreach.list

foreach unpacking with list()

$array = new array(
    "X" => ['one',2,3],
    "Y" => ['two',4,5]
);

// case 1:
foreach( $array as list($a, $b )) {  // Syntax error, unexpected T_LIST
    echo "\n$a $b";
}

// case 2:
foreach( $array as $key => list($a, $b, $c) ) {  // Syntax error, unexpected T_LIST
    echo "\n$key: $a $b - $c";
}

Output:

one 2
two 4
X: one 2 - 3
Y: two 4 - 5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions