How to get the first element of a SimpleXML object?

Edward Yu picture Edward Yu · Oct 12, 2013 · Viewed 7.8k times · Source

$xml is a SimpleXML object.

print_r($xml->Title);

outputs SimpleXMLElement Object ( [0] => K&H 3093 Extreme Weather Kitty Pad with Fleece Cover )

How do I access the first element?

print_r($xml->Title[0]);

outputs nothing!

Answer

Galen picture Galen · Oct 12, 2013

Try

echo (string) $xml->Title;

You have to cast it as a string.