3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = array( 'id' => 1, 'name' => 'Jacopo', 'male' => true, 'birthday' => new DateTime('1988-07-26'), 'hobbies' => array( 'Programming', 'TV Shows', 'Skiing' ) ); $rootXMLElement = '<data />'; $xml = new SimpleXMLElementExtended('<?xml version="1.0"?>' . $rootXMLElement); arrayToXML($data, $xml); echo $xml->asXML(); // http://coffeerings.posterous.com/php-simplexml-and-cdata class SimpleXMLElementExtended extends SimpleXMLElement { public function addCDATA($text) { $node = dom_import_simplexml($this); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($text)); } public function getParentNode() { return current($this->xpath('parent::*')); } } function arrayToXML($data, &$xml) { foreach ($data as $key => $value) { if (is_numeric($key)) { $key = 'item'; } if (is_array($value)) { if (!is_numeric($key)) { $node = $xml->addChild($key); arrayToXML($value, $node); } else { // $currentNodeName = $xml->getName(); // $currentNodeNameSingular = @Inflector::singularize($currentNodeName); // if ( $currentNodeName != 'data' // && $currentNodeName != $currentNodeNameSingular // ) { // $key = $currentNodeNameSingular; // } else { // $key = 'item'; // } $node = $xml->addChild($key); arrayToXML($value, $node); } } else { if (strpos($value, '<') === false) { $xml->addChild($key, $value); } else { $xml->addChild($key)->addCDATA($value); } } } }
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
Fatal error: Uncaught TypeError: strpos(): Argument #1 ($haystack) must be of type string, DateTime given in /in/YG4NO:61 Stack trace: #0 /in/YG4NO(61): strpos(Object(DateTime), '<') #1 /in/YG4NO(16): arrayToXML(Array, Object(SimpleXMLElementExtended)) #2 {main} thrown in /in/YG4NO on line 61
Process exited with code 255.
Output for 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
Warning: strpos() expects parameter 1 to be string, object given in /in/YG4NO on line 61 Warning: DOMDocument::createCDATASection() expects parameter 1 to be string, object given in /in/YG4NO on line 26 Fatal error: Uncaught TypeError: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given in /in/YG4NO:26 Stack trace: #0 /in/YG4NO(26): DOMNode->appendChild(NULL) #1 /in/YG4NO(64): SimpleXMLElementExtended->addCDATA(Object(DateTime)) #2 /in/YG4NO(16): arrayToXML(Array, Object(SimpleXMLElementExtended)) #3 {main} thrown in /in/YG4NO on line 26
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, 5.6.0 - 5.6.40
Warning: strpos() expects parameter 1 to be string, object given in /in/YG4NO on line 61 Warning: DOMDocument::createCDATASection() expects parameter 1 to be string, object given in /in/YG4NO on line 26 Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given, called in /in/YG4NO on line 64 and defined in /in/YG4NO on line 26
Process exited with code 255.
Output for 5.2.0 - 5.2.17
Catchable fatal error: Object of class DateTime could not be converted to string in /in/YG4NO on line 61
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6
Fatal error: Class 'DateTime' not found in /in/YG4NO on line 6
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/YG4NO on line 22
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
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/YG4NO on line 22
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/YG4NO on line 22
Process exited with code 255.

preferences:
296.46 ms | 401 KiB | 460 Q