3v4l.org

run code in 300+ PHP versions simultaneously
<?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 ($xml);
Output for 5.5.27 - 5.5.38, 5.6.11 - 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
object(SimpleXMLElement)#1 (1) { ["prod"]=> array(2) { [0]=> object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(1) { ["id"]=> string(1) "1" } ["layer"]=> array(3) { [0]=> object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_0" ["imgSRC"]=> string(20) "data/361_layer_0.png" } } [1]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_1" ["imgSRC"]=> string(20) "data/362_layer_0.png" } } [2]=> object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_2" ["imgSRC"]=> string(20) "data/363_layer_0.png" } } } } [1]=> object(SimpleXMLElement)#5 (2) { ["@attributes"]=> array(1) { ["id"]=> string(1) "2" } ["layer"]=> array(3) { [0]=> object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_0" ["imgSRC"]=> string(20) "data/361_layer_0.png" } } [1]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_1" ["imgSRC"]=> string(20) "data/362_layer_0.png" } } [2]=> object(SimpleXMLElement)#6 (2) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_2" ["imgSRC"]=> string(20) "data/363_layer_0.png" } ["layer"]=> object(SimpleXMLElement)#9 (1) { ["@attributes"]=> array(1) { ["id"]=> string(7) "layer_3" } } } } } } }
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.26, 5.6.0 - 5.6.10
object(SimpleXMLElement)#1 (1) { ["prod"]=> array(2) { [0]=> object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(1) { ["id"]=> string(1) "1" } ["layer"]=> array(3) { [0]=> object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_0" ["imgSRC"]=> string(20) "data/361_layer_0.png" } } [1]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_1" ["imgSRC"]=> string(20) "data/362_layer_0.png" } } [2]=> object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_2" ["imgSRC"]=> string(20) "data/363_layer_0.png" } } } } [1]=> object(SimpleXMLElement)#5 (2) { ["@attributes"]=> array(1) { ["id"]=> string(1) "2" } ["layer"]=> array(3) { [0]=> object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_0" ["imgSRC"]=> string(20) "data/361_layer_0.png" } } [1]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_1" ["imgSRC"]=> string(20) "data/362_layer_0.png" } } [2]=> object(SimpleXMLElement)#6 (2) { ["@attributes"]=> array(2) { ["id"]=> string(7) "layer_2" ["imgSRC"]=> string(20) "data/363_layer_0.png" } [0]=> object(SimpleXMLElement)#9 (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.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/Dtvfl on line 18
Process exited with code 255.
Output for 4.4.2, 5.1.2
Parse error: syntax error, unexpected '[' in /in/Dtvfl on line 19
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/Dtvfl on line 18
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/Dtvfl on line 18
Process exited with code 255.

preferences:
328.25 ms | 401 KiB | 452 Q