PHP code in a script is commonly delimited by opening <?php and closing ?> tags but there are also the older short open tags like this <? There are instances where you may need to disable short tags as shown in this post and using an Apache .htaccess file.
How to disable short tags
Create an .htaccess file in your website’s base directory, or use the existing one if you already have one, and add the following line:
php_value short_open_tag 0
This will now disable the use of <? and <?= in your PHP scripts. If your site only uses <?php tags then this is safe to do; if it uses <? as well then it is not safe to do as your scripts will no longer function as expected.
Why disable short tags?
I have an older website which is a bunch of static HTML files using the .html extension. I needed to do something a bit more clever with them but didn’t want to have to change all the extensions to .php and update all the links etc, so simply added .html as a PHP type. I have previously shown how to do this in my get Apache to parse .html files as PHP post.
Unfortunately, the DOCTYPE section of the HTML in all pages looked like this:
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
The issue if this is parsed as a PHP document and short tags are enabled, is that the PHP interpreter will see the <? part of the <?xml tag and think that part is PHP, resulting in this error:
Parse error: syntax error, unexpected T_STRING in ... on line ...
I needed to implement my change quickly so wasn’t about to mess around changing the DOCTYPE and retesting the site in different browsers, nor modify the first line to something like this:
<?php echo '<?xml version="1.0" encoding="iso-8859-1"?>'; ?>
So instead simply disabled short open tags and the error went away. Because the site didn’t already have any PHP in it, there were no existing tags to have to worry about; any new PHP code I added uses the <?php style tags instead.