3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = array('x' => array('value' => 'test')); $doc = new \SimpleXMLElement('<resource href="/"></resource>'); foreach ($data as $key => $value) { if (is_array($value) || $value instanceof \Traversable) { if (!is_numeric($key)) { if (count($value) > 0 && isset($value[0])) { $this->arrayToXml($value, $element, $key); } else { $subnode = $element->addChild($key); $this->arrayToXml($value, $subnode, $key); } } else { $subnode = $element->addChild($parent); $this->arrayToXml($value, $subnode, $parent); } } else { if (!is_numeric($key)) { if (substr($key, 0, 1) === '@') { $element->addAttribute(substr($key, 1), $value); } elseif ($key === 'value' and count($data) === 1) { $element->{0} = $value; } elseif (is_bool($value)) { $element->addChild($key, intval($value)); } else { $element->addChild($key, htmlspecialchars($value, ENT_QUOTES)); } } else { $element->addChild($parent, htmlspecialchars($value, ENT_QUOTES)); } } } echo $doc->x; //$xml = new \SimpleXMLElement('<resource href="/"><x><0>test</0></x></resource>'); //echo $xml->x;
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 $element in /in/GZnl1 on line 11 Fatal error: Uncaught Error: Call to a member function addChild() on null in /in/GZnl1:11 Stack trace: #0 {main} thrown in /in/GZnl1 on line 11
Process exited with code 255.
Output for 7.0.5 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: element in /in/GZnl1 on line 11 Fatal error: Uncaught Error: Call to a member function addChild() on null in /in/GZnl1:11 Stack trace: #0 {main} thrown in /in/GZnl1 on line 11
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught Error: Call to a member function addChild() on null in /in/GZnl1:11 Stack trace: #0 {main} thrown in /in/GZnl1 on line 11
Process exited with code 255.
Output for 7.0.0 - 7.0.4
Notice: Undefined variable: element in /in/GZnl1 on line 11 Fatal error: Uncaught Error: Call to a member function addChild() on unknown in /in/GZnl1:11 Stack trace: #0 {main} thrown in /in/GZnl1 on line 11
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Notice: Undefined variable: element in /in/GZnl1 on line 11 Fatal error: Call to a member function addChild() on null in /in/GZnl1 on line 11
Process exited with code 255.
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38
Notice: Undefined variable: element in /in/GZnl1 on line 11 Fatal error: Call to a member function addChild() on a non-object in /in/GZnl1 on line 11
Process exited with code 255.
Output for 5.0.4 - 5.0.5, 5.1.1 - 5.1.6, 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/GZnl1 on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/GZnl1 on line 6 Notice: Undefined variable: element in /in/GZnl1 on line 11 Fatal error: Call to a member function addChild() on a non-object in /in/GZnl1 on line 11
Process exited with code 255.
Output for 5.1.0
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/GZnl1 on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/GZnl1 on line 6 Fatal error: fatal flex scanner internal error--end of buffer missed in /in/GZnl1 on line 37
Process exited with code 255.
Output for 5.0.0 - 5.0.3
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/GZnl1 on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/GZnl1 on line 6 Notice: Undefined variable: element in /in/GZnl1 on line 11 Fatal error: Call to a member function addChild() on a non-object in /in/GZnl1 on line 11
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/GZnl1 on line 4 Parse error: syntax error, unexpected T_STRING in /in/GZnl1 on line 6
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
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/GZnl1 on line 4 Parse error: parse error, unexpected T_STRING in /in/GZnl1 on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/GZnl1 on line 4 Parse error: parse error in /in/GZnl1 on line 6
Process exited with code 255.

preferences:
339.67 ms | 401 KiB | 456 Q