3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $x = <<<XML <data> <prod id="1"> <layer id="layer_0" imgSRC="data/361_layer_0.png"/> <layer id="layer_1" imgSRC="data/362_layer_0.png"/> <layer id="layer_2" imgSRC="data/363_layer_0.png"/> </prod> <prod id="2"> <layer id="layer_0" imgSRC="data/361_layer_0.png"/> <layer id="layer_1" imgSRC="data/362_layer_0.png"/> <layer id="layer_2" imgSRC="data/363_layer_0.png"/> </prod> </data> XML; $xml = simplexml_load_string($x); // assuming XML in $x $node = $xml->xpath("//prod[@id='2']/layer[@id='layer_2']")[0]; $new = $node->addChild("layer",""); $new->addAttribute("id", "layer_3"); var_dump ($new);
Output for 5.4.0 - 7.1.0
object(SimpleXMLElement)#3 (1) { ["@attributes"]=> array(1) { ["id"]=> string(7) "layer_3" } }
Output for 4.4.3 - 4.4.9, 5.1.0 - 5.1.1, 5.1.3 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/nGbO2 on line 18
Process exited with code 255.
Output for 4.4.2, 5.1.2
Parse error: syntax error, unexpected '[' in /in/nGbO2 on line 19
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/nGbO2 on line 18
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/nGbO2 on line 18
Process exited with code 255.