Get value from SimpleXMLElement Object

Solution 1:

You have to cast simpleXML Object to a string.

$value = (string) $xml->code[0]->lat;

Solution 2:

You can also use the magic method __toString()

$xml->code[0]->lat->__toString()

Solution 3:

If you know that the value of the XML element is a float number (latitude, longitude, distance), you can use (float)

$value = (float) $xml->code[0]->lat;

Also, (int) for integer number:

$value = (int) $xml->code[0]->distance;

Solution 4:

if you don't know the value of XML Element, you can use

$value = (string) $xml->code[0]->lat;

if (ctype_digit($value)) {
    // the value is probably an integer because consists only of digits
}

It works when you need to determine if value is a number, because (string) will always return string and is_int($value) returns false