Friday, October 10, 2025
HomeLanguagesPHP | DOMNode hasChildNodes() function

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:




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


Output:

Yes, child nodes are there.

Program 2:




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


Output:

No, child nodes are not there.

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

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7099 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS