How to Enable PHP Short Tags

How to enable PHP short tags?

Set

short_open_tag=On

in php.ini

And restart your Apache server.

How can we enable php short-open-tag for a single script

TL;DR - No, those are the only two options you have.

If you can't do any of the mentioned methods, you will need a container script that sets the value before including the script with short open tags.

<?php

ini_set('short_open_tag', 'On');
include 'myscript.php';

This will prevent a parse error in myscript.php due to short open tags.

The documentation isn't very clear about this, but apparently this stopped working since PHP 4 after which it can only be changed using .htaccess or editing php.ini. This excerpt seems to imply that it might work from 5.3 onwards:

PHP_INI_ALL in PHP 4.0.0. PHP_INI_PERDIR in PHP < 5.3.0

But that's not the case, as can be seen from answers of Cannot turn off short_open_tag with ini_set

I've lodged a bug report for this documentation issue.

Update

The documentation will be updated to reflect this behaviour more explicitly:

... it's been PHP_INI_SYSTEM | PHP_INI_PERDIR since 4.0.1.

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.

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

How to enable Short Tags in Php7 in Alpine Linux?

php config in alpine is in /etc/php7/,

so you need to add something like

COPY ./php-overrides.ini /etc/php7/conf.d/

into your Dockerfile, and php-overrides.ini should have short_open_tag=on in it

How to enable PHP short_open_tag

Update: This was resolved by switching to a Scotch Box vagrant box.
Thanks all.

How to enable short tags in IIS 7.5

For ISS you need to go to "PHP Manager"
Then look for "Manage all settings"

Then search for short_open_tag
Double click on it and give value On

enter image description here

Follow the gif file

Restart the ISS and it will work.

Enable PHP short tags using .htaccess

Have you Apache directive AllowOverride All configured for this directory ?

PHP include runs when in short tags even when short tags are turned off

The included files are not read on the compilation phase but during runtime.

Since your PHP interpreter doesn't interpret the code in short tags and dumps it directly to the browser, it is not guilty for replacing the include statements with the content of the included files.

There is no php.ini setting that could persuade it to behave like this.

I can imagine other causes:

  • a PHP extension that replaces the include/require statements with the content of the included files;
  • a pre-processing script that does the same and/or combines multiple PHP files into a single one (Symfony does something similar);

The purpose of such a processing is to optimize the script by minimizing its disk access.



Related Topics



Leave a reply



Submit