3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $template = <<<XML <?xml version="1.0" encoding="utf-8"?> <resources></resources> XML; $writer = new SimpleXMLElement( $template ); $messages = array( 'key' => 'value', 'anotherkey' => 'anothervalue', ); foreach ( $messages as $key => $value ) { $element = $writer->addChild( 'string', $value ); $element->addAttribute( 'name', $key ); // This is non-standard if ( $key === 'key' ) { $element->addAttribute( 'fuzzy', 'true' ); } } // Make the output pretty with DOMDocument $dom = new DOMDocument( '1.0' ); $dom->formatOutput = false; $dom->preserveWhiteSpace = true; $dom->loadXML( $writer->asXML() ); echo $dom->saveXML();
based on NNcsc
Output for 5.1.3 - 7.2.0
<?xml version="1.0" encoding="utf-8"?> <resources><string name="key" fuzzy="true">value</string><string name="anotherkey">anothervalue</string></resources>
Output for 5.1.2
Fatal error: Call to undefined method SimpleXMLElement::addChild() in /in/vC9IZ on line 17
Process exited with code 255.
Output for 5.0.0 - 5.1.1
Fatal error: Call to undefined method SimpleXMLElement::addChild() in /in/vC9IZ on line 16
Process exited with code 255.
Output for 4.3.2 - 4.4.1, 4.4.3 - 4.4.9
Fatal error: Cannot instantiate non-existent class: simplexmlelement in /in/vC9IZ on line 8
Process exited with code 255.
Output for 4.4.2
Fatal error: Cannot instantiate non-existent class: simplexmlelement in /in/vC9IZ on line 9
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Cannot instantiate non-existent class: simplexmlelement in /in/vC9IZ on line 8