Skip to content

Instantly share code, notes, and snippets.

@Thinkscape
Last active June 11, 2021 02:53
Show Gist options
  • Save Thinkscape/6262156 to your computer and use it in GitHub Desktop.
Save Thinkscape/6262156 to your computer and use it in GitHub Desktop.
How to check if a <child> node exists in SimpleXMLElement document ? Comparison of different approaches.
<?php
/**
* The only reliable way of determining if a child exists
* in SimpleXMLElement is to use count(). All other methods
* do not work reliably in global or local NS.
*
* NOTE: Error suppresion on @count() is used to suppress
* "PHP Warning: count(): Node no longer exists"
*/
if(!class_exists('SimpleXMLElement')) die("Bonkers");
$xml = new SimpleXMLElement('
<xml xmlns:abstract="urn:my.org:abstract">
<abstract:node>foo</abstract:node>
<bar>123</bar>
</xml>
');
var_dump(gettype($xml->node));
var_dump(isset($xml->node));
var_dump(empty($xml->node));
var_dump(is_null($xml->node));
var_dump(@count($xml->node));
var_dump(@count($xml->node->children()));
echo "----------------------\n";
var_dump(gettype($xml->bar));
var_dump(isset($xml->bar));
var_dump(empty($xml->bar));
var_dump(is_null($xml->bar));
var_dump(@count($xml->bar));
var_dump(@count($xml->bar->children()));
echo "----------------------\n";
var_dump(gettype($xml->children('abstract',true)->node));
var_dump(isset($xml->children('abstract',true)->node));
var_dump(empty($xml->children('abstract',true)->node));
var_dump(is_null($xml->children('abstract',true)->node));
var_dump(@count($xml->children('abstract',true)->node));
var_dump(@count($xml->children('abstract',true)->node->children()));
echo "----------------------\n";
var_dump(gettype($xml->children('abstract',true)->bar));
var_dump(isset($xml->children('abstract',true)->bar));
var_dump(empty($xml->children('abstract',true)->bar));
var_dump(is_null($xml->children('abstract',true)->bar));
var_dump(@count($xml->children('abstract',true)->bar));
var_dump(@count($xml->children('abstract',true)->bar->children()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment