Extra Line In Html Template Causes Php Parser To Fail, When Short Tags Are Disabled

A case quite similar to this one, about doctype.

I wrote a non-Yii simple PHP app. It run without any problems on localhost and failed completely on webserver. I used mechanism very similar to Yii-one, for loading and parsing views stored in *.php file. I thought that problem will be there, but it turned out, that the source of the problem is first line of my template file:


<?xml version="1.0" encoding="iso-8859-2"?>

PHP’s short-tags were enabled on my localhost and disabled on web server, which caused described behaviour. PHP tried to parse this line and failed. Good question, why it has passed it by, without any problems, under XAMPP+Windows, with short tags enabled. In my opinion, no matter, if they’re enabled or they aren’t, this line is not parsable by PHP at all.

Since I use the very same mechanism, as Yii, for loading views and layouts, I took a quick look into Yii layout file (main.php) and found out that it doesn’t have this construction at first line.

So, question around app doctype. When we should use this line? Are XHTML documents still valid and go through validator without errors, if this line is removed? Is it necessary to have XHTML 1.1 Strict layout file? Finally – why some editors are adding this line, when creating new XHTML 1.1 Strict document, while others doesn’t?

The first 3 lines of my template are …




<?php echo '<?xml version="1.0" encoding="UTF-8" ?>' . "\n"; ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">



I once tried the following and got an error.




<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">