≡ Menu

How to enable PHP short tags?

The question – I have a lot of PHP files that I transferred from my old server to my new server. All of the PHP files have starting tag <? instead of <?php. How do I instruct PHP to recognize the <? tag as the PHP opening tag?

There is a PHP directive called short_open_tag which can be used to turn on support for short open tags (<? instead of <?php).

First find your php.ini file:

php -i | grep php.ini

This will output something like:

[root@server ~]# php -i | grep php.ini
PHP Warning: Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Moscow' for 'MSD/4.0/DST' instead in Unknown on line 0
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini
[root@server ~]#

So you php.ini is located at /etc/php.ini.

Now to check if you have short tags enabled, use the following command:

grep -i short /etc/php.ini

This will output something like:

[root@server ~]# grep -i short /etc/php.ini
; short_open_tag
; recommended for several years that you not use the short tag "short cut" and
; this short cut has been a feature for such a long time, it's currently still
; http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
short_open_tag = Off

This means that you have PHP short tags disabled.

Open the php.ini in your favorite editor:

vi /etc/php.ini

Then, replace the line shown above to the following:

short_open_tag = on

This will enable the support for short open tags!

Now, if you run PHP as an Apache module, you can also use .htaccess to do this.

Add the following line to your .htaccess file in your www or public_html directory.

php_value short_open_tag on

But it is recommended that you modify your php.ini if you have the right permissions. This will make the change globally for all PHP files that run on your server instead of the .htaccess way in which the change will be only for the directory in which your .htaccess resides.

Finally, restart your Apache httpd server:

service httpd restart

Comments on this entry are closed.