3v4l.org

run code in 300+ PHP versions simultaneously
<?php function fromXML(XMLReader $xml) { $node = []; while ($xml->read()) { // if (!$xml->isValid()) { // $xmlErrors = libxml_get_errors(); // $errorLines = array(); // foreach ($xmlErrors as $error) { // $errorLines[] = 'Line: ' . $error->line . ' Column: ' . $error->column . ' - ' . $error->message; // } // throw new Exception(join("", $errorLines)); // } switch ($xml->nodeType) { case XMLReader::END_ELEMENT: return (count($node) > 0) ? $node : null; case XMLReader::ELEMENT: $node[$xml->name] = ($xml->isEmptyElement ? '' : fromXML($xml)); break; case XMLReader::TEXT: case XMLReader::CDATA: $node = $xml->value; break; } } return $node; } $data = '<?xml version="1.0" encoding="UTF-8"?> <request> <header> <version>0.6</version> <dateTime>2017-07-05T11:21:46</dateTime> <sender>PROVIDER</sender> <recipient>dfsafdaf</recipient> <messageType>isvalid</messageType> </header> <voucher> <voucherId>1</voucherId> </voucher> </request>'; $xml = new XMLReader(); $xml->xml($data); var_dump(fromXML($xml));
Output for 8.0.10 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given in /in/0fd43:17 Stack trace: #0 /in/0fd43(19): fromXML(Object(XMLReader)) #1 /in/0fd43(19): fromXML(Object(XMLReader)) #2 /in/0fd43(19): fromXML(Object(XMLReader)) #3 /in/0fd43(47): fromXML(Object(XMLReader)) #4 {main} thrown in /in/0fd43 on line 17
Process exited with code 255.
Output for 8.0.0 - 8.0.9
Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, string given in /in/0fd43:17 Stack trace: #0 /in/0fd43(19): fromXML(Object(XMLReader)) #1 /in/0fd43(19): fromXML(Object(XMLReader)) #2 /in/0fd43(19): fromXML(Object(XMLReader)) #3 /in/0fd43(47): fromXML(Object(XMLReader)) #4 {main} thrown in /in/0fd43 on line 17
Process exited with code 255.
Output for 7.2.29 - 7.2.33, 7.3.16 - 7.3.33, 7.4.3 - 7.4.33
Warning: count(): Parameter must be an array or an object that implements Countable in /in/0fd43 on line 17 Warning: count(): Parameter must be an array or an object that implements Countable in /in/0fd43 on line 17 Warning: count(): Parameter must be an array or an object that implements Countable in /in/0fd43 on line 17 Warning: count(): Parameter must be an array or an object that implements Countable in /in/0fd43 on line 17 Warning: count(): Parameter must be an array or an object that implements Countable in /in/0fd43 on line 17 Warning: count(): Parameter must be an array or an object that implements Countable in /in/0fd43 on line 17 array(1) { ["request"]=> array(2) { ["header"]=> array(5) { ["version"]=> string(3) "0.6" ["dateTime"]=> string(19) "2017-07-05T11:21:46" ["sender"]=> string(8) "PROVIDER" ["recipient"]=> string(8) "dfsafdaf" ["messageType"]=> string(7) "isvalid" } ["voucher"]=> array(1) { ["voucherId"]=> string(1) "1" } } }
Output for 7.1.7
array(1) { ["request"]=> array(2) { ["header"]=> array(5) { ["version"]=> string(3) "0.6" ["dateTime"]=> string(19) "2017-07-05T11:21:46" ["sender"]=> string(8) "PROVIDER" ["recipient"]=> string(8) "dfsafdaf" ["messageType"]=> string(7) "isvalid" } ["voucher"]=> array(1) { ["voucherId"]=> string(1) "1" } } }

preferences:
120.99 ms | 402 KiB | 141 Q