phpphp-8php-shorttags

PHP 8 not revealing short_open_tag setting


The following returns a string length of zero:

<?php
print_r(ini_get('short_open_tag'));
?>

On two PHP 8 servers (apache2handler and cgi-fcgi) I need to know if short tags are enabled or not because it creates a conflict with XML. I can't afford to stop everything I'm doing to go look at phpinfo().

This code worked just fine in PHP 7.4 on both servers. Now, I do know that short tags are disabled though I need to be able to quickly know after server upgrades.

How do I get ini_get('short_open_tag') working again?


Solution

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