[php-src] Issue #21040: Issue with foreach when used with reference "&" and reusing the same variable
| From: | djidel | Date: | Mon, 26 Jan 2026 00:32:20 +0000 |
| Subject: | [php-src] Issue #21040: Issue with foreach when used with reference "&" and reusing the same variable | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-251602@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/21040
Author: djidel
### Description
The following code:
```php
<?php
$a = [1,2,3];
foreach($a as &$i) $i++;
var_export($a);
foreach($a as $i) $b = $i+1;
var_export($a);
```
Resulted in this output:
```
array (
0 => 2,
1 => 3,
2 => 4,
)array (
0 => 2,
1 => 3,
2 => 3,
)
```
But I expected this output instead:
```
array (
0 => 2,
1 => 3,
2 => 4,
)array (
0 => 2,
1 => 3,
2 => 4,
)
```
### PHP Version
```plain
Tested on 8.3.21, 8.4.10, 8.2.29, 8.1.33, 8.2.20:
```
### Operating System
_No response_