Skip to content
Related Articles

Related Articles

PHP | DOMNode hasChildNodes() function
  • Last Updated : 02 Mar, 2020

The DOMNode::hasChildNodes() function is an inbuilt function in PHP which is used to check if node has children or not.


bool DOMNode::hasChildNodes( void )

Parameters:This function doesn’t accept any parameter.

Return Value: This function returns TRUE on success or FALSE on failure.

Below given programs illustrate the DOMNode::hasChildNodes() function in PHP:

Program 1:

// Create a new DOMDocument
$dom = new DOMDocument();
// Create a paragraph element
$element = $dom->createElement('p', 'GeeksforGeeks!');
// Append the child
// Check if child nodes are there
if ($dom->hasChildNodes()) {
    echo "Yes, child nodes are there.";


Yes, child nodes are there.

Program 2:

// Create a new DOMDocument instance and keep it empty
$dom = new DOMDocument();
// Check if child nodes are not there
if (!$dom->hasChildNodes()) {
    echo "No, child nodes are not there.";


No, child nodes are not there.


My Personal Notes arrow_drop_up
Recommended Articles
Page :