How can the lack of a proper DOCTYPE declaration affect HTML validation in PHP?

Without a proper DOCTYPE declaration, HTML validation in PHP may not work correctly because the DOCTYPE declaration specifies the version of HTML being used, which is crucial for validation. To fix this issue, you need to ensure that your HTML document includes a valid DOCTYPE declaration at the beginning of the file.

<?php
// Set the DOCTYPE declaration for HTML5
$doctype = "<!DOCTYPE html>";

// HTML content with the DOCTYPE declaration
$htmlContent = $doctype . "
<html>
<head>
<title>Example Page</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>";

// Validate the HTML content
$validator = new W3CValidator();
$validationResult = $validator->validate($htmlContent);

// Check the validation result
if ($validationResult) {
    echo "HTML is valid!";
} else {
    echo "HTML is not valid.";
}
?>