3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = <<<XML <a> <b> <c>text</c> <c>stuff</c> </b> <d> <c>code</c> </d> </a> XML; // load source XML and create Xpath instance $source = new DOMDocument(); $source->loadxml($string); $xpath = new DOMXpath($source); // create target document with document element $target = new DOMDocument(); $root = $target->appendChild($dom->createElement('d')); // copy nodes from source to target foreach ($xpath->evaluate('/a/b/c') as $child) { $root->appendChild($target->importNode($child)); } // output target echo $target->saveXml();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Warning: Undefined variable $dom in /in/hlL1B on line 22 Fatal error: Uncaught Error: Call to a member function createElement() on null in /in/hlL1B:22 Stack trace: #0 {main} thrown in /in/hlL1B on line 22
Process exited with code 255.
Output for 7.3.16 - 7.3.31, 7.4.3 - 7.4.33
Notice: Undefined variable: dom in /in/hlL1B on line 22 Fatal error: Uncaught Error: Call to a member function createElement() on null in /in/hlL1B:22 Stack trace: #0 {main} thrown in /in/hlL1B on line 22
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught Error: Call to a member function createElement() on null in /in/hlL1B:22 Stack trace: #0 {main} thrown in /in/hlL1B on line 22
Process exited with code 255.
Output for 5.4.0 - 5.4.29, 7.2.29 - 7.2.33
Parse error: syntax error, unexpected end of file in /in/hlL1B on line 30
Process exited with code 255.
Output for 5.3.0 - 5.3.28
Parse error: syntax error, unexpected $end in /in/hlL1B on line 30
Process exited with code 255.

preferences:
137.2 ms | 401 KiB | 199 Q