3v4l.org

run code in 300+ PHP versions simultaneously
<?php $xml_string = '<?xml version="1.0"?> <result> <code> <name>Warszawa</name> <lat>52.25</lat> <lng>21.0</lng> <otherDetails>some Warsaw info here</otherDetails> </code> <code> <name>Somewhere Else</name> <lat>42</lat> <lng>99</lng> <otherDetails>some info about somewhere else here</otherDetails> </code> </result> '; $xml = simplexml_load_string($xml_string); echo "\n\n\$xml:\n\n"; print_r($xml); $codeZero = null; foreach ($xml->code->children() as $child) { $codeZero = $child; } echo "\n\n\$codeZero:\n\n"; var_dump($codeZero); $lat = null; foreach ($codeZero->children() as $child) { if (isset($child->lat)) { $lat = $child->lat; } } echo "\n\n\$lat:\n\n"; var_dump($lat);
Output for 7.0.0 - 7.0.23, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.29, 8.2.0 - 8.2.23, 8.3.0 - 8.3.11
$xml: SimpleXMLElement Object ( [code] => Array ( [0] => SimpleXMLElement Object ( [name] => Warszawa [lat] => 52.25 [lng] => 21.0 [otherDetails] => some Warsaw info here ) [1] => SimpleXMLElement Object ( [name] => Somewhere Else [lat] => 42 [lng] => 99 [otherDetails] => some info about somewhere else here ) ) ) $codeZero: object(SimpleXMLElement)#5 (1) { [0]=> string(21) "some Warsaw info here" } $lat: NULL

preferences:
81.38 ms | 409 KiB | 5 Q