[php-src] Issue #8073: Calling debug_backtrace in a foreach cycle restarts the foreach cycle
| From: | andrejsverza | Date: | Thu, 10 Feb 2022 08:22:16 +0000 |
| Subject: | [php-src] Issue #8073: Calling debug_backtrace in a foreach cycle restarts the foreach cycle | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-239601@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8073
Author: andrejsverza
### Description
The following code:
```php
<?php
function demo($array) {
$debug_backtrace = [];
foreach ($array as $key => &$value) {
echo "{$value}\r\n";
if ($value == 3) {
unset($array[$key]);
continue;
}
$debug_backtrace[] = debug_backtrace();
}
}
demo([1, 2, 3]);
```
Resulted in this output:
```
1
2
3
1
2
```
But I expected this output instead:
```
1
2
3
```
Please pay attention to usage of the ampersand for the $value.
Please note that commenting out the "$debug_backtrace[] = debug_backtrace();" will resolve
the issue.
This happens for PHP 8.0.15 as well.
### PHP Version
PHP 8.1.2
### Operating System
Windows 11 & Ubuntu 20.04