How can HTML code generated by PHP scripts be validated for correctness?

When generating HTML code using PHP scripts, it's essential to ensure that the generated HTML is valid and follows the correct structure. One way to validate the HTML code is by using an online HTML validator tool like the W3C Markup Validation Service. This tool will check the generated HTML code for any errors or inconsistencies and provide feedback on how to fix them.

// Example PHP code snippet to generate HTML code and validate it using the W3C Markup Validation Service

$html = "<html><head><title>Example</title></head><body><h1>Hello World!</h1></body></html>";

// Output the generated HTML code
echo $html;

// Validate the HTML code using the W3C Markup Validation Service
$validator_url = "https://validator.w3.org/nu/?doc=" . urlencode($html);
echo "<p>Validate your HTML code <a href='$validator_url' target='_blank'>here</a></p>";