| aharvey | No | 2012-08-20 01:44 |
| derick | Yes | 2012-08-18 09:27 |
| googleguy | No | 2012-08-21 17:07 |
| hradtke | Yes | 2012-08-18 04:59 |
| ircmaxell | Yes | 2012-08-18 12:17 |
| kassner | Yes | 2012-08-19 16:10 |
| kriscraig | Yes | 2012-08-19 07:07 |
| laruence | Yes | 2012-08-25 12:30 |
| levim | Yes | 2012-08-18 16:11 |
| nikic | Yes | 2012-08-19 08:58 |
| rdohms | Yes | 2012-08-22 12:42 |
| seld | Yes | 2012-08-19 07:51 |
| stas | No | 2012-08-19 04:30 |
| willfitch | No | 2012-08-20 15:00 |
| zhangzhenyu | Yes | 2012-08-24 10:07 |
| aharvey | No | 2012-08-20 01:44 |
| googleguy | No | 2012-08-21 17:07 |
| ircmaxell | No | 2012-08-19 12:25 |
| kassner | No | 2012-08-19 16:09 |
| kriscraig | Yes | 2012-08-19 07:07 |
| laruence | Yes | 2012-08-25 12:30 |
| levim | No | 2012-08-20 13:49 |
| nikic | No | 2012-08-19 08:58 |
| rdohms | No | 2012-08-22 12:42 |
| stas | No | 2012-08-19 07:47 |
| willfitch | No | 2012-08-20 14:58 |
| zhangzhenyu | No | 2012-08-24 10:07 |
Copied from wiki.php.net, last synchronized Oct 13, 2026 11:52 UTC
This feature introduces list() support in foreach constructs:
<?php
$users = array(
array('Foo', 'Bar'),
array('Baz', 'Qux'),
);
// Before
foreach ($users as $user) {
list($firstName, $lastName) = $user;
echo "First name: $firstName, last name: $lastName. ";
}
// After
foreach ($users as list($firstName, $lastName)) {
echo "First name: $firstName, last name: $lastName. ";
}
This feature eliminates the use of a redundant variable ($user in the example above), reduces code verbosity in typical cases of iterating structured data, such as SQL result sets, and it doesn't introduce new keywords, but simply reuses a familiar PHP construct in a new context.
It's a commonly requested feature, and there is evidence that people already expect list() should work in this scenario: #10203 allow foreach($array as list($a,$b)
This RFC provides a behavior specification and implementation for this feature.
In order to avoid the reduce/reduce conflict, new bison rules will be added to the existing “foreach_variable”, to avoid this side effect:
<?php
foreach (array(1,3,4) as &$key => $foo) {
}
Without the new patch:
PHP Parse error: syntax error, unexpected '&', expecting T_STRING or T_VARIABLE or '$' in /home/huixc/test.php on line 2
With the new patch:
PHP Fatal error: Key element cannot be a reference in /home/huixc/test.php on line 2
Fatal error: Key element cannot be a reference in /home/huixc/test.php on line 2
It is possible to add support of the silent token in the new context:
$array = array(array(1,3,4), array(1, 2));
foreach ($array as @list($a, $b, $c)) {
} 1
Vote: Should this RFC be merged into trunk? — Yes: 11 · No: 4 (closed)
Vote: Should this RFC(with supporting of silent token) be merged into trunk? — Yes: 2 · No: 10 (closed)