PHP error in .xml file?

OK firstly, I added this line to my .htaccess file so the php engine parses php code in .xml file:

AddType application/x-httpd-php .php .php3 .phtml .html .xml

After that when I view an .xml file I get this PHP error:

Parse error: parse error, unexpected T_STRING in /var/www/vhosts/mydomain.com/httpdocs/test.xml on line 1

But line 1 is not even php infact this is line 1:

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

Can anyone tell me what the problem is?

Thanks.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

That’s because <? is the short opening tag for php.

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

Solution 2

Yeah, sad but true, it’s the doctype being seen as the PHP opening short tag. You have a few options:

  1. Take XML out of the list of files for PHP to parse in the .htaccess file
  2. Wrap all of your xml declarations in PHP:

    <?php print('<?xml version="1.0" encoding="utf-8"?>'); ?>

  3. Turn off short_open_tag in .htaccess or the php.ini. This means that PHP will only accept <?php as an opening tag and will ignore <?.

Solution 3

Alternative 4, in addition to daniel’s: drop the <?xml…?> declaration completely.

Since ‘1.0’ and ‘utf-8’ are the defaults for XML, it’s completely redundant; including the XML declaration here gains you nothing.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply