Why do I have to declare Go To StackoverFlow.com

2

I think that I've been used to a fairly liberal policy regarding the PHP declaration in projects - I've always just used:

<? // here is my php code ?>

I just setup an nginx server using PHP-FPM under FastCGI and now it requires me to declare explicitly:

<?php // here is my php code ?>

Is there any way to change that? (Since otherwise I would have to go into my project and find/replace all of the instances where this occurs).

2012-04-04 18:07
by jpea
shortopentag, though it isn't recommended to use.. - Michael Berkowski 2012-04-04 18:08
@Michael, from 5.4 version is always enabled. So yes, it's recommended to use it - Daniel Ribeiro 2012-04-04 18:10
@drgomesp Incorrect - with 5.4, the output shorthand <?= is always available, while short_open_tag is off by default - Michael Berkowski 2012-04-04 18:11
@Michael Thanks for the correction - Daniel Ribeiro 2012-04-04 18:12


13

You need to enable short open tags via the php.ini configuration file. However, if you are using XML concurrently with PHP, there will be a conflict.

Just found a good article on the deprecation of short tags in PHP6.

2012-04-04 18:08
by Evan Mulawski
I would add the caviate that PHP, in the newer versions, is doing away with short tags. So you will have to use the tag. Whether you like it or not. Also it really isn't hard to do, so why are you complaining - Patrick 2012-04-04 18:13
It's not that it's hard to do, but it's annoying to migrate a legacy project where short open tags were used in favor of the proper declarative - jpea 2012-04-04 18:15
seriously ? If I were you, I would just open all those files in a code text editor (like notepad++) and replace all <? with <?php. It takes about 2 minutes.. - Radu Murzea 2012-04-04 18:25


5

Go into php.ini and find the option short_open_tag. Set it to On. This will solve your problem.

2012-04-04 18:09
by Radu Murzea
4 answers in a single minute... cool.. - Radu Murzea 2012-04-04 18:10


4

Short open tags are what you're looking for.

2012-04-04 18:09
by Crontab


1

There is no such thing as a PHP script. There's only files that have PHP code blocks embedded in them. You MUST start a php code block with <?php (or <? if you've got short-tags enabled). There is no way around this. Without that opening tag, the PHP interpreter will just act as a very expensive version of 'cat'.

2012-04-04 18:13
by Marc B


1

While the answers given are correct in how to enable short tags, I would recommend against it. Consider that the code you write may not be run solely on your computer. If you plan on distributing your code or deploying it to a production server you should always use <?php to open the tags as this will minimize the likelihood of your code not running and the troubleshooting associated with it.

2012-04-04 18:15
by Crashspeeder