Hi Guys,

When I am using XML version in the XHTML, then I am getting parser error [Parse error: parse error, unexpected T_STRING on line 1].

<?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" xml:lang="en-US" lang="en-US">
<head>
<title>Testing Page</title>
</head>
<body>
<p>Hi</p>
</body>
</html>

When I store the above code under .php extension, I am getting parser error.

Observations:
- When I stored the same code under .htm extension, I am not getting any error.
- When I remove the version and encoding and just use <?xml?> in the first line and use .php extension, I am not getting any error.

System:
Apache/2.0.50 (Win32)
PHP Version 4.3.8 [Build Date Jul 13 2004 17:31:56]

Can you please help in understand what is going on and why parser error. I like to use the version in XML just to conform with w3c.org XHTML standards.

Dani AI

Generated

Short answer: the XML prolog is being mistaken for a PHP short-open tag, so the PHP parser chokes on the literal <?xml ... ?>. As found, that only happens when the file is parsed by PHP (e.g. saved as .php); when the XML line is left in an unparsed .htm file there is no PHP parse step. This behaviour is the classic short‑open‑tag conflict. (stackoverflow.com)

Useful diagnostic points and why some fixes work: the short_open_tag directive controls whether <? (not <?php) is treated as PHP and cannot reliably be flipped at runtime in many setups; it is intended to be set at the server or per-directory level (php.ini, httpd.conf, .user.ini or .htaccess when using mod_php). The phpinfo() output shows the active value for short_open_tag. Changing it with ini_set() is not dependable for this option on many PHP builds. (php.net)

Practical alternatives (depending on hosting and requirements): prefer disabling short tags on the server if possible; allow the XHTML to be emitted by PHP code (i.e., output the XML declaration from PHP after the interpreter is active); serve the page as static markup if no PHP is needed; or map/enable server parsing for the file extension used so that the file is handled consistently (note that Apache’s server‑parsed / handler configuration is what was leveraging). Which option is feasible depends on whether PHP runs as an Apache module, via FPM/CGI, or is under shared hosting. (httpd.apache.org)

Context and best practice: short tags have long been discouraged (and have been the subject of deprecation discussion in PHP), so code that relies on <? should be avoided for portability; use long tags (<?php) and output XML prologs from inside PHP where server settings prevent literal <?xml at the top of a parsed file. (wiki.php.net)

Summary checklist: confirm the file is actually parsed by PHP, check short_open_tag via phpinfo(), and then either disable short tags at the server level or change how the XML prolog is emitted or which extensions/handlers the server uses.

I am giving this answer for future reference for somebody looking for the same answer.

The syntax is correct, but the following pages gave me the information I need:
http://www.php.net/manual/en/language.basic-syntax.php

The answer is:
[1] Either disable short_open_tag in the php.ini. [But not always we have the option as webhosting company might not be willing to change.]
[2] The alternative is to use this

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

I am still looking for other solutions, but may do well to add this...

If the page is not being server-parsed I receive the same error.
So for instance, take a .htm file with the xml declaration,
when I add...
AddHandler server-parsed .htm
...the error goes away.

Cheers,
TwoHawks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.