[php-src] Issue #11244: Since PHP 8, throwing Exception inside recursive foreach loop causes reset of internal pointer

From: Date: Mon, 15 May 2023 17:06:56 +0000
Subject: [php-src] Issue #11244: Since PHP 8, throwing Exception inside recursive foreach loop causes reset of internal pointer
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-244455@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/11244
Author: survik1

### Description

Hello, we are finally pushing our code to PHP 8 and I have encountered this BC break which seems
more like bug than feature to me. This behavior is still present in 8.2 and the change was not
documented (unless i missed it ofc).

https://3v4l.org/aNejW

```php
<?php

$_ENV = [
    'ZASIS_UI_BACKGROUNDCOLOR' => '#FFF1EC',
    'ZASIS_UI_INFOPANEL_ENABLE' => '1',
    'ZASIS_UI_INFOPANEL_TEXT' => 'DEBUG IS ON',
    'ZASIS_UI_INFOPANEL_BACKGROUNDCOLOR' => '#F44',
];

$arr = [
    'ui' => [
        'backgroundColor' => 'ZASIS_UI_BACKGROUNDCOLOR',
        'infoPanel' => [
            'enable' => 'ZASIS_UI_INFOPANEL_ENABLE',
            'text' => 'ZASIS_UI_INFOPANEL_TEXT',
            'fontColor' => 'ZASIS_UI_INFOPANEL_FONTCOLOR',
            'backgroundColor' => 'ZASIS_UI_INFOPANEL_BACKGROUNDCOLOR',
        ]
    ]
];

function loadEnv(&$data): void
{
    if (is_array($data)) {
        foreach ($data as $key => &$value) {
            try {
                loadEnv($value);
            } catch (\InvalidArgumentException $e) {
                unset($data[$key]);
            }
        }
    } elseif (array_key_exists($data, $_ENV)) {
        $data = $_ENV[$data];
    } else {
        throw new \InvalidArgumentException();
    }
}
loadEnv($arr);
print_r($arr);
```

Output in PHP >=8.0:
```
Array
(
    [ui] => Array
        (
            [backgroundColor] => #FFF1EC
            [infoPanel] => Array
                (
                    [backgroundColor] => #F44
                )

        )

)
```

Output in PHP >=7.1 <8.0:
```
Array
(
    [ui] => Array
        (
            [backgroundColor] => #FFF1EC
            [infoPanel] => Array
                (
                    [enable] => 1
                    [text] => DEBUG IS ON
                    [backgroundColor] => #F44
                )

        )

)
```


### PHP Version

PHP 8.2.6

### Operating System

_No response_


Thread (1 message)

  • survik1
« previous php.bugs (#244455) next »