Php: "Short_Open_Tag = On" Not Working

PHP 7.2.14 short_open_tag = On ignored

I will first note that this question looks like a duplicate of this one. On the off chance it's not, here's my best answer.

The PHP documentation has a page on the opening tags which says:

PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).

Check phpinfo() and you may see a section entitled Configure Command which contains compilation options. See if there's an --enable-short-tags option in there anywhere. If not, listen to @phil and look for a section titled Additional .ini files parsed which may list other ini files that have been parsed.

If your search is as thorough as you suggest in your original post and you still cannot get short tags, it may be turned off in an apache configuration file or the short_open_tag directive may exist in more than one spot. A PHP.ini directive will override any prior values that might have been set.

A grep search might help. In my PHP info output, I see these values:

Loaded Configuration File - /etc/php5/apache2/php.ini

Scan this dir for additional .ini files - /etc/php5/apache2/conf.d

I can easily search all files in that location with this grep command:

grep -ir 'short_open_tag' /etc/php5/apache2

If you double/triple check all these ini files and restart apache and still can't get short_open_tag setting to work, this value may be set as an apache option. I suggest searching the apache configuration files for any reference to short_open_tag. The exact directory location may be different on your machine, but this grep command works for me

grep -ir 'short_open_tag' /etc/apache2

You should also keep in mind that your apache configuration may not be set up to even bother with .htaccess files so your attempt to override using .htaccess may be for naught unless you configure apache to actually use the .htaccess file. Assuming your apache configuration does bother parsing your htaccess file, this appears to be covered in another question here on SO.

And finally, if your server is configured to use PHP-FPM, then it uses a pool of PHP processes to handle PHP requests from the web server. If that's the case, you would need to restart php-fpm with this command:

sudo service php7.0-fpm restart

NOTE: this command may vary on different machines.

How to enable PHP short tags?

Set

short_open_tag=On

in php.ini

And restart your Apache server.

PHP 8 not revealing short_open_tag setting

Although this is a boolean setting, ini_get does in fact return a string.

Note: When querying boolean values

A boolean ini value of off will be returned as an empty string or "0" while a boolean ini value of on will be returned as "1". The function can also return the literal string of INI value.

So when the feature is enabled, it will return "1" which you can use to check the status of the setting. (Despite the last sentence in that note, I've never seen any literal values such as "on" or "off" returned for boolean values.)

var_dump(ini_get('short_open_tag'));

Output with the setting enabled:

string(1) "1"

A little history: although there was a vote taken to remove these tags, it was overturned. The chief concern was with legacy code that would have been displayed to end users when the server was upgraded – the same reason the setting continues to default to "On" despite its use being discouraged for decades. After the meltdown over the first RFC, a subsequent proposal that made short open tags an error condition did not receive the required 2/3 majority to pass.

short tags doesn`t work despite i have changed short_open_tag = On (i have mac os x)

You should look something like below:

; 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 = Off

Below is just a comment :

; short_open_tag
; Default Value: On
; Development Value: Off
; Production Value: Off

Also restart web server after updation.

php short_open_tag problem

Normally you write PHP like so: <?php PHP CODE HERE ?>. However if allow_short_tags directive is enabled you're able to use: <? PHP CODE HERE ?>. Also sort tags provides extra syntax: <?= $var ?> which is equal to <?php echo $var ?>.

Short tags might seem cool but they're not. They causes only more problems. Oh... and IIRC they'll be removed from PHP6.

short_open_tag setting ignored by PHPUnit

So, I got it to work and the solution is frustrating: My application is on a VM and the php.ini file within the VM had the correct setting. However, PHPUnit was using the php.ini file on my local machine, which had short_open_tag set to Off. Changing that setting fixed my problem.

(I'm still unsure of why PHPUnit uses the other php.ini, but I think that's outside the scope of this question.)

Enable PHP short open tags via .htaccess

Try this

 <IfModule mod_php5.c>
php_value short_open_tag 1
</IfModule>

This would solve the problem



Related Topics



Leave a reply



Submit