The XML Prolog
July 16th, 2008
If you’re serving an XHTML file, the W3C states: “XHTML document authors are strongly encouraged to use XML declarations in all their documents.” The XML declaration, in this case being the dreaded XML prolog: <?xml version='1.0' encoding='utf-8'?>. But, it’s really not so dreadful unless you’re using PHP. The server-side PHP processor sees the <? and starts to process the XML prolog as PHP code, which it isn’t. This throws an error like:
Parse error: syntax error, unexpected T_STRING in /foo/index.html on line 1
Zeldman says avoid the prolog and use the metatag: “specify character encoding by inserting a Content-Type element into the
of your document.” But, this isn’t what the W3C says to do and I always waffle: follow the W3C and risk breaking a website or don’t follow the W3C and fail at compliance.One work around of the PHP problem is to echo the XML prolog:
<?php echo "<?xml version='1.0' encoding='utf-8'?>" ?>
That’s what I’m doing for now. It follows W3C recommendation, let’s the page work with PHP processing and doesn’t require reconfiguration of PHP settings like short-open-tag.
Forums abound where people recommend abandoning the XML prolog altogether (usually for IE6 quirks mode reasons), but I’d prefer to go the standards route wherever (humanly) possible. And since the death of IE6 (Microsoft’s IE6 page directs you to download IE7), going the standards route is getting easier.