php SimpleXML check if a child exists

It might be better to wrap this in an isset()

if(isset($A->b->c)) { // c exists

That way if $A or $A->b don't exist... it doesn't blow up.


SimpleXML always return Object. If there is no child, empty object is returned.

if( !empty($a->b)){
  var_dump($a->b);
}

After some experimentation, I've discovered that the only reliable method of checking if a node exists is using count($xml->someNode).

Here's a test case: https://gist.github.com/Thinkscape/6262156

Tags:

Php

Simplexml