Enable PHP short open tags in PHP.ini

How to Enable PHP Short Open Tag (short_open_tag)?

Last updated on | 5 replies

In this article we will enable the short_open_tag option in the PHP configuration to allow us to use the short tag <? as well as <?php.

Introduction

PHP short open tags is a deprecated feature of PHP and it’s been recommended for several years that you not use the short tag “short cut” and instead to use the full <?php and ?> tag combination. But because this short cut has been a feature for such a long time, it’s currently still supported for backwards compatibility.

You may have upgraded PHP recently or moved your web app to a different server and been hit with a PHP fatal error similar to below:

PHP message: PHP Parse error: syntax error, unexpected end of file, expecting elseif (T_ELSEIF) or else (T_ELSE) or endif (T_ENDIF) 

If you don’t have time to replace every instance of <? with <?php and test your web app, you can tell PHP to interpret <? as <?php with the short_open_tag option.

1. Locate php.ini

Firstly, you need to locate your php.ini file. In this example, our php.ini is located in /etc/php/7.4/apache2/php.ini, however, this may be different for you depending on your PHP version. If you are unsure, please read:

2. Apache

Edit the PHP config. Replace php7.4 with your own version, e.g, php5.6php7.1, etc. (Use ls /etc/php/ if you are not sure which version is installed.)

sudo nano /etc/php/7.4/apache2/php.ini

Not there? If you are using PHP FPM, php.ini might be in /etc/php/7.4/fpm/php.ini

Search for short_open_tag = (Press CTRL + W to search in nano)

Change it to:

short_open_tag = On

Save changes and close nano (Press CTRL + X and then press y and ENTER)

Restart Apache.

sudo systemctl restart apache2

If you are using PHP-FPM, you must restart that service. Replace php7.4 with your own version, e.g, php5.6php7.1, etc.

sudo service php7.4-fpm restart

3. Nginx or Apache with PHP-FPM

Edit the PHP config. Replace php7.4 with your own version, e.g, php5.6php7.4, etc.

sudo nano /etc/php/7.4/fpm/php.ini

Search for short_open_tag = (Press CTRL + W to search in nano)

Change it to:

short_open_tag = On

Save changes and close nano (Press CTRL + X and then press y and ENTER)

Restart PHP-FPM. Replace php7.4 with your own version, e.g, php5.6php7.4, etc.

sudo service php7.4-fpm restart

short_open_tag

With the wide spread use of XML and use of these tags by other languages, the server can become easily confused and end up parsing the wrong code in the wrong context. The short_open_tag setting tells PHP whether the short form (<? ?>) of PHP’s open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>).

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

5 replies

Leave a reply

Your email address will not be published. Required fields are marked *

  1. OH MY GAD !
    sudo service php7.2-fpm restart
    HELPS ME!

    Before this i was trying to restart nginx with sudo service php7.2-fpm restart and it was restarted but php.ini changes not appllied.

    Thank you very much!