XML declaration within PHP

I create my RSS(XML) documents using PHP, so the information is always dynamic.

However, I ran into a problem when attempting to use the standard XML declaration, in my XML documents:

Since the XML declaration is similar to the PHP declaration,

... a big fat parse error is the only obvious output.

As you might guess, the question mark is what causes a bump. To get around this, one of two things has to be done:

  1. Make a small change in my php.ini file, or an .htaccess file.
  2. Echo a true string literal to the page, so PHP doesn’t parse the question marks.

Echo a string literal

Since I don’t have (or I am not sure if I have) access to my php.ini file, I went with option 2:

print ("\n");

or...

echo '\n';

This seems to correct the issue.

php.ini or .htaccess

However, I still would like to know how to do it with step 1. I have done some research from the PHP end, and it seems there is a setting within the php.ini file, which needs to be turned on or off.

Also, I have heard there is a line in an .htaccess file that can do the same thing.

I will be looking into it.

9 thoughts on “XML declaration within PHP

  1. When you use
    in a single-quoted string (‘) it will not be parsed as a newline, remember that.
    And btw: look in to short-tags ;) (for php.ini/htaccess)

  2. What IS the htaccess script that I need?

    And, everyone, please ignore the ‘echo’ example from above… I totally messed that one up. Like Blitz said, I don’t need the newline (
    ), and I don’t even think I need to escape those double quotes… but I could be wrong on that one.

  3. echo ‘< ?xml version="1.0" encoding="utf-8"?>
    ‘;
    ive added < ?php ?> at both ends it does it for me! thanks!

  4. Thanks, Matt. However, your suggestion does not fully work.

    This does:

    <?php echo "<?xml version="1.0" encoding="utf-8"?>
    "; ?>

  5. None of your suggestions work fully. I have tried all the above suggestions, it opens the page without error BUT at the very top-left corner of the page it prints “; ?>
    How do you fix that then?

  6. The following has been working for me, for years. I have used Dreamhost and Media Temple as web hosts.

    In .htaccess file:

    AddType php-cgi .xml
    

    And in your PHP file:

    <?php
    
    	header("Content-type: text/xml");
    	print ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
    
    ?>
    

    Hope that helps clear this one up!

Comments are closed.