Getting Values from SimpleXML – A Mystery!

Here’s a tip I can’t really explain. Since I’m on the road and quality programming time is in shorter supply, I don’t have a lot of time to figure it out, so I’m hoping someone will read this and tell me why this works. I’m just glad it does!

The problem is that sometimes it’s not so easy to get values out of the SimpleXML object – at least not from the file I’m trying to read from. It looks like a standard XML file, but for some reason – probably something I’m doing wrong – it just doesn’t want to give up the value. If I just use something like this:

$var = $xml->element;

That returns me another simpleXML object. I don’t want an object, I want a string.

My first solution works, but it’s tedious and ugly:

foreach($xml->element as $e) {
  $var = trim($e[0]);

I finally found a more elegant solution by accident:

$var = strtolower($xml->element);

I’m not sure what the strtolower function does to make it work and, at this point, it’s one of those times where I don’t have time to find out, so I would love it if one of you would solve that little mystery for me! Obviously, somehow it’s converting it to a string type without giving me an error. This will bug me until I figure it out, so someone please, save me some time and tell me how it does that!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top