Search code examples
phphtmlw3c-validation

W3C validator: "Element head is missing a required instance of child element title"


I'm trying to validate my document for HTML5 using the W3C validator at http://validator.w3.org/check. When I run the code below using "direct input" I get error messages like Element head is missing a required instance of child element title and Stray start tag html. This baffles me, because the <html> tag is right there. Why can't the validator see it?

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="some content">
<title>page title</title>
</head>
<body>
</body>
</html>

I'm using PHP to generate parts of the file.


Solution

  • When I viewed it in a hex editor I discovered two null bytes (hex code 0x00) before the <html> tag exactly where my first <?php ?> tag was. When I copied the code or ran it by URL, the validator encountered the null bytes. When I wrote the code manually, it validated fine.

    <!DOCTYPE html>
    <?php
    /* the PHP tag apparently outputs two null bytes */
    ?>
    <html>
    <head>
    <title>page title</title>
    </head>
    <body>page body</body>
    </html>
    

    I moved the first PHP tag down to the first line of the body and the validation passed.