How can the hasChildNodes function be utilized to handle empty XML containers in PHP?

When working with XML data in PHP, empty XML containers can cause issues when trying to access child nodes. To handle this situation, the hasChildNodes function can be used to check if the container has any child nodes before attempting to access them. This helps prevent errors and allows for more robust XML parsing.

$xml = '<root></root>';

$doc = new DOMDocument();
$doc->loadXML($xml);

if ($doc->documentElement->hasChildNodes()) {
    // Access child nodes here
} else {
    // Handle empty container
}