Note that supplying the same node for $newnode and $refnode leads to an E_WARNING ("Couldn't add newnode as the previous sibling of refnode"). For example imagine one wanted to make $newnode the first child of its parent by doing:
<?php
$firstSibling = $newnode->parentNode->firstChild;
$newnode->parentNode->insertBefore( $newnode, $firstSibling );
?>
This would generate a warning if it already was the first child of its parent, since $newnode and $firstSibling are identical. Easy to work around though:
<?php
$firstSibling = $newnode->parentNode->firstChild;
if( $newnode !== $firstSibling ) {
$newnode->parentNode->insertBefore( $newnode, $firstSibling );
}
?>