# RFC: foreach_variable supporting T_LIST

- Status: Implemented (PHP 5.5)
- Authors: Xinchen Hui laruence@php.net
- Date: 2011-07-06
- Version: 1.0
- Original: https://wiki.php.net/rfc/foreachlist
- Page: https://externals.io/rfc/foreachlist
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Should this RFC be merged into trunk? (closed)

Yes: 11 · No: 4

Voters: aharvey (No), derick (Yes), googleguy (No), hradtke (Yes), ircmaxell (Yes), kassner (Yes), kriscraig (Yes), laruence (Yes), levim (Yes), nikic (Yes), rdohms (Yes), seld (Yes), stas (No), willfitch (No), zhangzhenyu (Yes)

### Should this RFC(with supporting of silent token) be merged into trunk? (closed)

Yes: 2 · No: 10

Voters: aharvey (No), googleguy (No), ircmaxell (No), kassner (No), kriscraig (Yes), laruence (Yes), levim (No), nikic (No), rdohms (No), stas (No), willfitch (No), zhangzhenyu (No)

## Discussions on the mailing list

- [\[RFC\] foreach_variable supporting T_LIST token](https://externals.io/message/53757.md) — started on 2011-07-06, 7 messages
- [\[Patches\]#36944, #54556, #55211(and svn karma applying)](https://externals.io/message/54189.md) — started on 2011-07-25, 5 messages
- [\[RFC\] foreach_variable supporting T_LIST](https://externals.io/message/61402.md) — started on 2012-07-18, 8 messages
- [\[VOTE\]Call for voting: support use list in foreach](https://externals.io/message/62232.md) — started on 2012-08-18, 27 messages

---

## Introduction

This feature introduces list() support in foreach constructs:

```php
<?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)](https://bugs.php.net/bug.php?id=10203)

This RFC provides a behavior specification and implementation for this feature.

## Implementation details of the RFC

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
<?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
```

## Possible additional features (vote separately): silent token

It is possible to add support of the silent token in the new context:

```php
$array = array(array(1,3,4), array(1, 2));
foreach ($array as @list($a, $b, $c)) {
}                                   1
```

## Patches

- foreach list: [https://github.com/laruence/php-src/tree/foreachlist\_nosilenttoken](https://github.com/laruence/php-src/tree/foreachlist_nosilenttoken)
- foreach list with supporting of silent token: <https://github.com/laruence/php-src/tree/foreachlist>

## Vote

**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)

## Changelog

- 2011-07-06 Xinchen Hui: Initial RFC creation
- 2011-07-06 Xinchen Hui: Updated patch
- 2011-07-06 Xinchen Hui: Added tests phpt
- 2011-07-07 Xinchen Hui: Added supporting for slience token(@)
- 2011-07-07 Xinchen Hui: Updated sencode patch
- 2011-07-08 Xinchen Hui: Added opcodes with silent token
- 2011-07-24 Xinchen Hui: Updated patches that fixed a bug: znode-&gt;EA may be depend on an uninitialized value (thanks to Felipe)
- 2012-07-18 Phidev: Rewrote the RFC
- 2012-08-18 Xinchen Hui: Open voting
- 2012-08-25 Xinchen Hui: Close voting
