$xml = new DOMDocument(); $xml->loadXML(''); $root = $xml->documentElement; if ($root->hasChildNodes()) { echo "The root element has child nodes."; } else { echo "The root element has no child nodes."; }
$html = new DOMDocument(); $html->loadHTML('This code creates an HTML document with a div element containing a paragraph element, and then checks if the div element has any child nodes. The output would be "The div element has child nodes." In both examples, the DOMNode hasChildNodes method checks if the current node has any child nodes using an if-else statement, and then outputs the result. The DOMNode class is part of the PHP DOM library.'); $div = $html->getElementsByTagName('div')->item(0); if ($div->hasChildNodes()) { echo "The div element has child nodes."; } else { echo "The div element has no child nodes."; }Hello, world!