361

I have a web application on a Linux server which starts with <?

I needed to copy this application to a windows environment and everything is working fine except that an SQL statement is being rendered differently. I don't know if this has to do with the script beginning with <?php instead of <? because I don't know from where to enable the <? from the PHP.ini so I changed it to <?php

I know that these 2 statements are supposed to mean the same but I need to test it with <? in order to ensure that the application is exactly the same. This way I can eliminate another possibility.

Thanks

samayo
  • 16,163
  • 12
  • 91
  • 106
seedg
  • 21,692
  • 10
  • 42
  • 59
  • 8
    [Short open tags are deprecated in 7.4 and shall be removed in 8.0.](https://wiki.php.net/rfc/deprecate_php_short_tags) – bishop Apr 24 '19 at 19:59
  • 2
    However, the story is [still developing](https://marc.info/?l=php-internals&m=155617657127015&w=2). Stay tuned. :) – bishop Apr 25 '19 at 08:10

21 Answers21

618

Set

short_open_tag=On

in php.ini

And restart your Apache server.

Raj
  • 22,346
  • 14
  • 99
  • 142
codaddict
  • 445,704
  • 82
  • 492
  • 529
89

This can be done by enabling short_open_tag in php.ini:

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_value short_open_tag 1

For the people thinking that short_open_tags are bad practice as of php 5.4 the <?= ... ?> shorttag 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

RJD22
  • 10,230
  • 3
  • 28
  • 35
  • 5
    Short open tags are not necessarily bad practice (I'm trying to keep an open mind here) but just because Rasmus et al. decided to provide default support for them in PHP5.4 does not provide justification for using them. Using them might create a dependency which is undesirable later on or cause confusion with XML. Some good discussion here: http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use?rq=1 – Ian Lewis Jul 09 '13 at 08:32
  • 1
    Ian. We are not talking about the ` echo $var ?>` short open tag but the `= $var ?>` short open tag. Afaik XML should not be affected. – RJD22 Jul 10 '13 at 08:12
  • We have a PHP coding test and occasionally receive submissions where the = short tag has been used. Unfortunately the assumption that this style is in use everywhere is a little naive and often comes from developers brought up on a diet of ASP. Clearly it is available but its use is a micro-optimisation and will fail on portability grounds, which is a shame. – Ian Lewis Jul 10 '13 at 13:36
  • 8
    @IanLewis I don't agree. The PHP short tag has nothing to do with ASP. The best reason why you would want to use `= ` is for templating and I think it's a fairly valid one. Other templating languages like mustache also use short simple tags `{{var}}`. PHP is often used as a templating language and having to do `` is so much uglier than `=$var?>` in inline HTML. – RJD22 Jul 10 '13 at 13:43
  • It's an interesting debate and becomes highly subjective, I don't believe beautiful code is necessarily better than code that shows a bit of the inherent ugliness. My own view here is that the =$var?> version looks much more crowded and much less readable than the more open, but longer, version. The link with ASP must be a common perception then as several developers I know all make the same link without prompting. – Ian Lewis Jul 10 '13 at 14:33
  • This should be php_flag instead of php_value since it's setting a boolean. – Julian Sep 05 '17 at 23:54
  • @IanLewis We can use space to make `=$var?> less crowed. See official example: https://www.php.net/manual/en/language.basic-syntax.phpmode.php#example-40 –  Sep 14 '19 at 12:00
  • Interpolation is cleaner than concatenation. `"Hi {$name}! Last login: {$date}."` is better than `"Hi ".$name."! Last login: ".$date."."`. When generating content, you want the focus to be on the content itself. The same applies for when using PHP for templating. `Hi =$name?>! Last login: =$date?>.` is ugly, but it's better than `Hi ! Last login: .` – obe Jan 04 '21 at 17:19
26

This can be done by enabling short_open_tag in php.ini:

1.To locate php.ini file,on comment line execute

 php --ini

you will get some thing like this,

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.

2.Open php.ini file and find short_open_tag. By default it is in off change it to on.

3.Restart the server,execute this comment

service httpd restart

Thanks

user2086641
  • 4,331
  • 13
  • 56
  • 96
18

To set short tags to open from a Vagrant install script on Ubuntu:

sed -i "s/short_open_tag = .*/short_open_tag = On/" /etc/php5/apache2/php.ini
Bradley Flood
  • 10,233
  • 3
  • 46
  • 43
14

I can see all answers above are partially correct only. 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

Go to php.ini and set short_open_tag = On

IMPORTANT: then you must restart your php-fpm process so this can work!

sudo service php-fpm restart

and then finally restart your nginx/http server

sudo service nginx restart
Eddy Ferreira
  • 770
  • 8
  • 14
  • 2
    In My Case : sudo service php7.0-fpm restart – Dumindu Perera Jun 03 '16 at 03:47
  • sudo service php-fpm restart will work for any default version of PHP you have install on your box, unless you have multiple versions in which case you must specify the version you are referring to – Eddy Ferreira Jun 04 '16 at 17:21
  • 1
    Restarting nginx only didn't do the trick for me. In fact, the phpini() function continued showing short_open_tag as Off. I just rebooted the server and it worked. Kind of drastic, but still faster than research why... – Fran Marzoa Jun 20 '17 at 13:01
10

As simple, as that, follow the following steps:

  1. Go to php.ini file
  2. Find short_open_tag and set it to on

    short_open_tag = On
    
  3. Restart the server

Eje
  • 354
  • 4
  • 8
10

you need to turn on short_open_tags.

short_open_tag = On
Jage
  • 7,990
  • 3
  • 32
  • 31
8

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:

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_value[short_open_tag] =  On
xakru
  • 269
  • 2
  • 4
  • Had to do this on a Debian Jessie. This might apply to all PHP5-FPM installs actually. – Bertrand Jan 21 '16 at 12:18
  • That's because you don't include `php.ini` into `php-fpm.conf`. – miken32 May 20 '21 at 20:14
  • @miken32 , there no way to include php.ini into php-fpm.conf. Your comment could confuse the reader. The answer was written eight years ago, I think you shouldn’t have commented on it, the answer might be out of date. – xakru Jun 18 '21 at 13:42
  • To say "you can't set short_open_tag in /etc/php.ini for php-fpm" is wrong, and it was wrong 8 years ago. This is in your output: "Unable to include /etc/php.ini from /etc/php-fpm.conf at line 159" which means you were trying to include php.ini into php-fpm.conf. – miken32 Jun 18 '21 at 15:22
  • @miken32 , You’re right, it wasn’t that, I’d forgotten what was here eight years ago. – xakru Aug 10 '21 at 15:15
7

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.
6

For Wamp Server users there is easier way: You may enable that setting simply (left) click once on the WampServer icon, choose PHP -> PHP settings -> short open tag. Wait for a second, then WampServer will automatically restart your PHP and also its web service.

originally from: http://osticket.com/forums/showthread.php?t=3149

Reza Ameri
  • 1,803
  • 3
  • 24
  • 32
6

if you edit your php.ini file, remember to restart your service (apache2, etc) for the edits to php.ini to take effect

Steve Wasiura
  • 768
  • 1
  • 8
  • 19
5
; 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
Rhadley
  • 65
  • 1
  • 5
3

You can follow the following steps:

1-> Go to php.ini file inside /etc/php/7.3/apache2 or inside your PHP version and

2-> Find short_open_tag and set it to On and removing ; from starting.

short_open_tag = On

3-> Restart the server

sudo service apache2 restart
Vishal Thakur
  • 1,564
  • 16
  • 25
1

if using xampp, you will notice the php.ini file has twice mentioned short_open_tag . Enable the second one to short_open_tag = On . The first one is commented out and you might be tempted to uncomment and edit it but it is over-ridden by a second short_open_tag

Roger Gajraj
  • 523
  • 4
  • 12
1

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

Praful Rajput
  • 367
  • 3
  • 12
1
sed -i "s/short_open_tag = .*/short_open_tag = On/" /etc/php/7.2/apache2/php.ini

That works on php7.2 on ubuntu 16, same answer as above by Bradley Flood, although the directory in which the config file is stored has changed.

Also you can change the version in the php string to match your currently installed version.

Raymie
  • 109
  • 1
  • 9
0

For docker add this step to Dockerfile

  ARG phpIniPath=/path/to/your/php.ini

  RUN sed -i -e 's/^short_open_tag\s*=.*/short_open_tag = On/' $phpIniPath  
-1

To enable short_open_tag for a particular domain with php-fpm, you must edit :

/etc/php5/fpm/pool.d/xxxxxxxxxx.conf

Where xxxxx is the socket number of the domain.

And add : php_value[short_open_tag] = On

contremaitre
  • 106
  • 1
  • 2
  • 9
-1
 short_open_tag = On

in php.ini And restart your Apache Server.

-2

I'v Changed the short_open_tag Off to On on my aws centos 7 instance and php7(PHP 7.0.33 (cli) (built: Dec 6 2018 22:30:44) ( NTS )), but its not reflecting the php info page and the code. So I refer may docs and find a solution on my case. Add an extra line after the short_open_tag as asp_tags = On after that restart Apache It works on the code and I go the output correctly

php.ini file

engine = On

; This directive determines whether or not PHP will recognize code between
; <? and ?> tags as PHP source which should be processed as such. It is
; generally recommended that <?php and ?> should be used and that this feature
; should be disabled, as enabling it may result in issues when generating XML
; documents, however this remains supported for backward compatibility reasons.
; Note that this directive does not control the <?= shorthand tag, which can be
; used regardless of this directive. 
; Default Value: On   
; Development Value: Off     
; Production Value: Off  
; http://php.net/short-open-tag

short_open_tag = On

; Allow ASP-style <% %> tags   
; http://php.net/asp-tags
asp_tags = On
bibincatchme
  • 333
  • 4
  • 10
-5

Set the asp_tags = On and short_open_tag = On in both the files \apache\Apache2.2.21\bin\php.ini and \bin\php\php5.3.8\php.ini and then restart the apache server.

kal
  • 48
  • 5
  • 1
    asp is most probably a typo. Why can't the community show just a little mercy for @kal? – n8bar Apr 07 '17 at 18:32