PHP

[ Solved -10 Answers] PHP How to enable PHP short tags

How to enable PHP short tags -If you don't have access to the php.ini you can try to enable them trough the .htaccess file

  • We have a web application on a Linux server which starts with <?
  • we needed to copy this application to a windows environment and everything is working fine except that an SQL statement is being rendered differently.
  • we don’t know if this has to do with the script beginning with <?php instead of <? because we don’t know from where to enable the <? from the PHP.ini so we changed it to <?php
  • we know that these 2 statements are supposed to mean the same but we need to test it with <? in order to ensure that the application is exactly the same.
  • This way we can eliminate another possibility.

  • This can be done by enabling short_open_tag in php.ini:
php code
 short_open_tag = on
  • If you don’t have access to the php.ini you can try to enable them trough the .htaccess file but it’s possible the hosting company disabled this if you are on shared hosting:
php code
php_value short_open_tag 1
  • For the people thinking that short_open_tags are bad practice as of php 5.4 the <?= … ?> short tag will supported everywhere, regardless of the settings so there is no reason not to use them if you can control the settings on the server. Also said in this link: short_open_tag

  • Set in php.ini
php code
short_open_tag=On
  • And restart your Apache server.

  • To locate php.ini file,on comment line execute
php code
php –ini

you will get some thing like this,

php code
Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed:      /etc/php.d/curl.ini,
/etc/php.d/fileinfo.ini,
/etc/php.d/gd.ini,
/etc/php.d/json.ini,
/etc/php.d/mcrypt.ini,
/etc/php.d/mysql.ini,
/etc/php.d/mysqli.ini,
/etc/php.d/pdo.ini,
/etc/php.d/pdo_mysql.ini,
/etc/php.d/pdo_sqlite.ini,
/etc/php.d/phar.ini,
/etc/php.d/sqlite3.ini,
/etc/php.d/zip.ini

See 2nd line from the comment output.The file will be in the mentioned path.

  • Open php.ini file and find short_open_tag. By default it is in off change it to on.
  • Restart the server,execute this comment
READ  [ Solved -5 Answers ] PHP - Maximum length of the textual representation of an IPv6 address?

service http restart

  • To set short tags to open from a Vagrant install script on Ubuntu:
php code
sed -i "s/short_open_tag = .*/short_open_tag = On/" /etc/php5/apache2/php.ini

  • In reality all 21st Century PHP apps will have FastCGI Process Manager(php-fpm) so once you have added php-info() into your test.php script and checked the correct path for php.ini
php code
 Go to php.ini and set short_open_tag = On
  • IMPORTANT: then you must restart your php-fpm process so this can work!
php code
sudo service php-fpm restart

and then finally restart your nginx/http server

php code
  sudo service nginx restart

As simple, as that, follow the following steps:

  1. go to php.ini file
  2. find, ‘short_open_tag’ and set it to on,
  3. restart the server

so, short_open_tag = On

In CentOS 6(tested on Centos 7 too) you can’t set short_open_tag in /etc/php.ini for php-fpm. You will have error:

php code
ERROR: [/etc/php.ini:159] unknown entry 'short_open_tag'
ERROR: Unable to include /etc/php.ini from /etc/php-fpm.conf at line 159
ERROR: failed to load configuration file '/etc/php-fpm.conf'
ERROR: FPM initialization failed

You must edit config for your site, which can found in /etc/php-fpm.d/www.conf And write at end of file:

php code
php_value[short_open_tag] =  On

If you are using Ubuntu with Apache+php5, then on current versions there are 2 places where you need to change to short_open_tag = On

  1. /etc/php5/apache2/php.ini – this is for the pages loaded through your web server (Apache)
  2. /etc/php5/cli/php.ini – this configuration is used when you launch your php files from command line, like: php yourscript.php – that goes for manually or cronjob executed php files directly on the server.
READ  PHP - Convert from MySQL datetime to another format with PHP

If you are using xampp in windows then please do following

1.Open XAMPP control panel.

2.Click on CONFIG button.

3.Go to PHP (php.ini) option.

  • Find short_open_tag using ctrl+f utility

You will found ; short_open_tag

  • kindly remove the semicolon (;) from line.

and keep it as short_open_tag = on

Finally, restart your Apache server

php code
; Default Value: On
; Development Value: Off
; Production Value: Off
; http://php.net/short-open-tag
;short_open_tag=Off   <--Comment this out
; XAMPP for Linux is currently old fashioned
short_open_tag = On   <--Uncomment this
shareimprove this answer
answered Jan 9 '14 at 11:38

 

 

 

About the author

Venkatesan Prabu

Venkatesan Prabu

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

Add Comment

Click here to post a comment

X