PHP 8.5.0 Alpha 4 available for testing

Voting

: max(nine, eight)?
(Example: nine)

The Note You're Voting On

Michael D (DigitalGemstones.com)
17 years ago
Note that iterating through the childNodes array and removing those children will stop the iteration.

For example:
<?php
foreach($node->childNodes as $child)
{
if(
iWantToDeleteThisNode($child))
$child->parentNode->removeChild($child);
}
?>

Will not work. Note that in removing the node, the childNodes array gets rebuilt (it seems) and so only the first item will be removed. In order to properly remove all the children you want to remove, you will need to do something like the following:

<?php
$nodesToDelete
= array();
foreach(
$node->childNodes as $child)
{
if(
iWantToDeleteThisNode($child))
$nodesToDelete[] = $child;
}
foreach(
$nodesToDelete as $node)
{
$node->parentNode->removeChild($node);
}
?>

I believe this is actually more efficient than the first snippet would be (if it worked) but obviously I cannot benchmark it.

<< Back to user notes page

To Top