How can one ensure that the $htmlString variable is not empty when using DOMDocument::loadHTML() in PHP?

When using DOMDocument::loadHTML() in PHP, it is important to ensure that the $htmlString variable is not empty to avoid errors. To prevent this, you can check if the variable is not empty before passing it to the loadHTML() method. This can be done using a simple if statement to validate the input.

// Check if $htmlString is not empty before loading it with DOMDocument::loadHTML()
if (!empty($htmlString)) {
    $dom = new DOMDocument();
    $dom->loadHTML($htmlString);
    
    // Rest of your code here
} else {
    echo "Error: HTML string is empty.";
}