[php-src] Issue #13863: Removal during NodeList iteration breaks loop
| From: | nielsdos | Date: | Mon, 01 Apr 2024 18:28:27 +0000 |
| Subject: | [php-src] Issue #13863: Removal during NodeList iteration breaks loop | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246693@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/13863
Author: nielsdos
### Description
The following code:
```php
<?php
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->loadXML(
<<<EOXML
<x>
<item1 />
<item2 />
<item3 />
<item4 />
</x>
EOXML
);
foreach ($doc->documentElement->childNodes as $i => $node) {
echo $node->localName . PHP_EOL;
if ($i === 2) {
$node->remove();
}
}
```
Resulted in this output:
```
item1
item2
item3
```
But I expected this output instead:
```
item1
item2
item3
item4
```
Source: https://github.com/nielsdos/php-src/pull/93#discussion_r1546665771
Credits to @veewee. Similar issue exists for attributes.
### PHP Version
Since always
### Operating System
_No response_