demosthenes.info

Iā€™m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter or add me on Google+.

web developer guide

my books

Book cover of Pro CSS3 AnimationPro CSS3 Animation, Apress, 2013

my projects

A Sass color keyword system for designers. Replaces CSS defaults with improved hues and more memorable, relevant color names.

CSSslidy: an auto-generated #RWD image slider. 3.8K of JS, no JQuery. Drop in images, add a line of CSS. Done.

tipster.ioAutomatically provides local tipping customs and percentages for services anywhere.

Interweaving XML and PHP: The Prolog Problem

php / introduction

Estimated reading time: 1 minute, 30 seconds

Take a simple XHTML page and save it as prologtest.php, with the code below:

<?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" lang="en">
<head>
<title>An XHTML 1.0 Strict standard template</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
</body>
</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:

Parse error: syntax error, unexpected T_STRING in 
/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 a little flexibility, 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 the XML prolog. Replace the first line of the page with the following code (note the uses of single quotes):

    <?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.

comments powered by Disqus

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.