The Naked Green

2005.3.3 Thursday

PHP Includes and XML

Filed under: Web Design — Mr. Green @ 5.54 pm

Today I realized that I could use PHP includes to make a more modular website design almost like a template. What I realized was that I could use PHP without knowing anything about it! So I went crazy adding <?php include('filename.txt'); ?> all over the place and breaking up my XHTML into smaller bite size pieces. I quickly learned that the file location is from the server root (/home/user/public_html/filename.txt) and not the URL.

A problem that came up (there has to be at least one) was with my <?xml version="1.0" encoding="iso-8859-1"?> line at the top of my document. It was causing my newly made PHP document to break. I figured it was because of it being similar to the way you begin a PHP statement. I didn’t want to get rid of it is part of W3C’s Recommendation for XHTML 1.1. I found out found the answer had to do with the short_open_tag variable….

The PHP configuration file (php.ini) has a variable called short_open_tag. By default, it is set to On.

When short_open_tag is turned on, you can abbreviate the opening tag in a PHP statement to <?. This is to allow for backwards compatibility with some older SGML applications. However, using this shortcut is strongly discouraged unless you have a really good reason to use it. Saving yourself a few characters of code is not a good reason.

In use short tags look like this:

<? some php statements ?>

If you want to code to XML standards, short tags are a problem, since they will cause the PHP processor to try to treat all XML processing directives as PHP statements, whether they are or not.

For instance, if you are writing XML documents, they should begin with an XML prologue that identified the document type. It takes the form of:

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

If short tags are turned in, then the PHP processor will try to process this and generate a string of errors. You end up having to write it like this:

<?php
if ($stricttest) {
    echo "&lt?xml version="1.0" encoding="UTF-8"x3f>";
    echo "n";
    }
?>

Which can cause its own problems with any XML applications trying to process the document.

If you want to work with XML, make sure that short_open_tag is set to Off. If you are planning on porting code to unknown platforms, then you definitely do not want to use short tags.
–more–

Once I figured out the syntax (short_open_tag = off), I put it into to a file I named php.ini and put it in the website’s /public_html/ folder. Presto, no more problems!

Getting some CSS to work in Internet Exploder today was quite another issue (I fixed it after a few hours, but don’t quite know how).

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

www.TheNakedGreen.com © 2023