3v4l.org

run code in 300+ PHP versions simultaneously
<?php $doc = new DOMDocument; $doc->loadHTML('<html><body id="x"></body></html>'); $body = $doc->getElementById('x'); $div = $doc->createElement('div'); $div->setAttribute('id', 'y'); echo '0: ' . ( $doc->getElementById('y') ? $doc->getElementById('y')->tagName : 'null' ) . PHP_EOL; // div $body->appendChild($div); echo '1: ' . ( $doc->getElementById('y') ? $doc->getElementById('y')->tagName : 'null' ) . PHP_EOL; // div $div->setIdAttribute( 'id', true ); echo '2: ' . ( $doc->getElementById('y') ? $doc->getElementById('y')->tagName : 'null' ) . PHP_EOL; // null
based on pi599
Output for 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.21, 7.4.0 - 7.4.10, 8.0.0alpha1 - beta3
0: div 1: div 2: null