PHP | DOMNode hasChildNodes() function

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

Syntax:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Yes, child nodes are there.

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// 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.";
}
?>

chevron_right


Output:

No, child nodes are not there.

Reference: https://www.php.net/manual/en/domnode.haschildnodes.php

My Personal Notes arrow_drop_up

I love Open Source Culture have contributed to phpMyAdmin, LibreOffice, Ant-Design and Web is my passion

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.