In PHP a SimpleXMLElement which represents an empty XML element (self-closing tag or empty open/close tag pair with no content) casts to boolean FALSE
. This is perhaps a bit unexpected, as normally every object in PHP casts to boolean TRUE
:
var_dump((bool) new SimpleXMLElement("<guid/>")); # bool(false)
var_dump((bool) new SimpleXMLElement("<guid></guid>")); # bool(false)
var_dump((bool) new SimpleXMLElement("<guid> </guid>")); # bool(true)
This special rule is documented in the PHP manual on Converting to boolean.
You can make use of that to check whether or not the <guid>
element you have is empty. However it's important here, you ask for the element specifically. In your existing code:
$guid = $alerts->channel->item->guid;
you're not asking for a specific <guid>
element, but for all which are children of the parent <item>
element. These kind of SimpleXMLElement objects cast to boolean true unless they contain zero elements (compare with your use of SimpleXMLElement::count()
).
Different to that, if you obtain the first <guid>
element by index, you will get a SimpleXMLElement of that index or NULL
in case the element does not exist (that means, there is no <guid>
element).
Both - non-existing element as NULL
or the existing, empty one - will cast to boolean false
which can be easily used in your if/else statement:
$guid = $alerts->channel->item->guid[0];
### zero is the index of the first element
if ((bool) $guid) {
# non-empty, existing <guid> element
} else {
# empty or non-existing
}
This then answers your question.