demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

Interweaving XHTML and PHP: The XML Prolog Problem

Let's take a simple page and save it as prologtest.php, with the code below:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <head>
  6. <title>An XHTML 1.0 Strict standard template</title>
  7. <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  8. </head>
  9. <body>
  10. </body>
  11. </html>

As before, upload this page to your server and use your browser to look at it in that location.

Depending on how your server is set up (error messages shown or repressed) you will see either nothing at all on the page, or an error line similar to the following:

  1. Parse error: syntax error, unexpected T_STRING in
  2. /f2/demosthenes/public/test.php on line 1

This demonstrates two important points:

  1. When PHP doesn't work (almost always due to an error in your code) it really doesn't work: the page stops, and you get an error message, or nothing at all.

  2. The echo statement prints information to the page: that information could be a text string, as we have above, HTML, a variable, or a combination of all three. Anything other than a variable must be enclosed inside quotes; either single quotes ('') or double quotes("").

So what's the problem with our page?

The PHP opening tag is usually <?php ..., but it may also be shortcut to <? .... This latter, optional format for the opening tag confuses the PHP parser, which attempts to interpret our opening XML prolog as PHP. There are several possible solutions to this:

  1. Drop the XML prolog. Doing so sacrifices some flexibility and future-proofing, avoids an issue in IE6 (which also has an issue with the XML prolog), keeps your page valid, and is probably the quickest solution.

  2. Set the PHP parser to only accept the long form of the opening PHP tag and ignore the short version.

  3. Use PHP to echo out the XML prolog. Replace the first line of the page with the following code (note the uses of single quotes):

    1. <?php echo '<?xml version="1.0" encoding="utf-8"?>' ?>

Choose the option that appeals to you, make the appropriate change, save and upload the page, and view it again.

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.