I got a very awkward and specific issue with a simplexml evaluation.
The code:
$simplexml = simplexml_load_string($xmlstring);
var_dump($simplexml);
var_dump($simplexml == false); //this comparison
var_dump($simplexml) returns the actual structure of my simplexml but the comparison returns ‘true’ for this specific simplexml, which I can’t show the structure because of my contract.
I’m sure that’s very specifc issue ’cause I tried other XML strings and the comparison returns ‘false’.
$simplexml = simplexml_load_string('<a><b>test</b></a>');
var_dump($simplexml); //returns the actual structure
var_dump($simplexml == false); //returns false
I solved the problem using the ‘===’ operator, but I’m not satisfied with just making it work. I want to understand why the ‘==’ operator returns true.
I read about the two operators and the SimpleXMLElement and on my sight it should return ‘false’ for both operators.
What are the possible reasons for a comparison between a succesfully parsed SimpleXMLElement and the boolean ‘false’ to return ‘true’?
Have a look here:
http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
It says that SimpleXML objects created from empty tags evaluate to false. Maybe that’s what’s going on?