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.6
, php7.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.6
, php7.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.6
, php7.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.6
, php7.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.
Just to avoid any confusion, as per https://www.php.net/manual/en/language.basic-syntax.phptags.php:
As short tags can be disabled it is recommended to only use the normal tags ( and ) to maximise compatibility.
This means that the setup is not disabled by setting short_open_tag to Off.
In other words, only the form is disabled, not the form.
Thanks it really helped me
Thank you very much!!!
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!
In PHP 7.3 (and upper) directive can be double (in one more places) – be aware about that.