2

Why doesn't this page validate?

http://www.jethroweb.nl/test/test.php

I think the XHTML code is okay, but the W3C Markup Validation Service and the WDG HTML Validator do not agree.

UPDATE: The XHTML code is generated by PHP, this are the first lines of code:

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"';
echo PHP_EOL;
echo '        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
echo PHP_EOL;
echo '<html xmlns="http://www.w3.org/1999/xhtml">';
echo PHP_EOL;
echo '<head>';
echo PHP_EOL;

UPDATE2: Maybe it's more a PHP question. When I paste the generated XHTML code into Notepad++ I see a question mark at the first row:

enter image description here

waanders
  • 8,907
  • 22
  • 70
  • 102
  • @DarkDust: You just edited the question so that it no longer contains the information needed to answer it. The code that you pasted doesn't contain the byte order mark that is the actual problem, so it validates just fine. – Guffa Oct 14 '11 at 07:45

3 Answers3

2

As per the W3C validator

Byte-Order Mark found in UTF-8 File.

The Unicode Byte-Order Mark (BOM) in UTF-8 encoded files is known to cause problems for some text editors and older browsers. You may want to consider avoiding its use until it is better supported.

That's the reason for your error: the byte order mark is a zero-width space, and it's telling you that character isn't allowed in the prolog (before the doctype).

Re-save the file with a text editor that allows saving without a BOM (virtually everything except Notepad).

Community
  • 1
  • 1
Michael Madsen
  • 54,231
  • 8
  • 72
  • 83
  • I did save my PHP source file in Notepad++ with the 'Encoding > Convert to UTF-8 without BOM' option and now the XHTML code validates fine! – waanders Oct 14 '11 at 08:48
  • I was wondering if it's also possible with the PHP header() function to force to not use a BOM? – waanders Oct 14 '11 at 08:50
  • @waanders: No. The BOM is part of the file itself, and PHP isn't aware of it - if a BOM is present, PHP will simply think it's part of the output. That also means `header` actually *breaks* - from PHP's perspective, the output has started, and that means it's too late to modify the HTTP header of the response. – Michael Madsen Oct 14 '11 at 09:28
  • But the PHP sourcefile exists on the server, only the HTML output is sent to the browser. The BOM is no part of the generated HTML. Or...? – waanders Oct 14 '11 at 10:07
  • @waanders: Everything not enclosed in `` is part of the generated HTML; you don't need to use PHP to output static text. The BOM is placed at the very start of the file, i.e. before the initial ` – Michael Madsen Oct 14 '11 at 11:26
2

Because the file is sent with a byte order mark.

The byte order mark is used to identify the encoding for text files, but they should not be included when you send the text over the web.

If your web server can't remove the byte oder mark when it sends the file, you have to save the file without the byte order mark. Most editors have the option to do so in their Save As dialog.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • But it's no static XHMTL file, it is generated by PHP. I'll add the PHP code in my question – waanders Oct 14 '11 at 07:52
  • @waanders: Then the BOM is in the PHP markup file, and is passed through in the output as static content. It's just treated as any other text outside the PHP code markers. – Guffa Oct 14 '11 at 09:35
  • Thanks for your help, the issue is solved, see comment answer Michael Madsen – waanders Oct 14 '11 at 10:05
2

Your php source file test.php contains the byte order mark. Try to save test.php again without byte order mark.

xdazz
  • 158,678
  • 38
  • 247
  • 274