The following code does not work as expected:
$xml = simplexml_load_string("text");
$node = $xml->xpath("/root/a")[0];
if ($node) process($node); // process($node) may not be called for some valid nodes
Unlike most other objects, SimpleXmlElement may evaluate as false even when it is not null. Specifically, SimpleXmlElements representing empty
(
Read more... )
What does "goes inside the “if”" mean?
In your example "$node" is inside of "if", right?
Do you mean that if XPath found empty element, then resulting $node would be null?
Or you mean that if input XML contains empty node, then XPath search result would always evaluate to null even if it found not empty node?
Reply
I fixed the example, so it is now (hopefully) clearer.
Reply
Of course.
> I fixed the example
I do not see what you fixed.
If your xpath was "/root/a" instead of "/root/b" it would be more clear I guess.
But your current xpath example is searching for node that is not empty.
Reply
The 'goes inside if' part.
> But your current xpath example is searching for node that is not empty.
Well, that's the bug I found. The node is not empty, but PHP thinks it is. But I agree, it's confusing in this setting. Changed query to /root/a.
Reply
1) Weird interpretation of "if(object)".
2) Xpath bug that sometimes treats non-empty nodes - as if they were empty.
Right?
Reply
Leave a comment