How to retrieve boolean values from PHP's configuration

It might seem trivial to retrieve boolean settings from php's configuration with the use of ini_get(). As an example, we will try to know if php is running in safe_mode or not. There is a ini directive for this, which can be defined in php.ini or in httpd.conf .

in php.ini :

safe_mode = 0

in httpd.conf :

php_admin_value safe_mode 0

Now, let's try to guess its value from a php script.

The wrong but obvious way

if (ini_get('safe_mode') == false) { echo "Safe mode disabledn";}

This seems to work in Pokemon if you are a dojo master, but not in real life.

The problem

Here is an extract of the documentation of ini_get() :

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.

Which basically means nothing. After some tests, here is an understandable summary of its real behavior :

If your setting is defined in php.ini, an empty string is returned when the setting is disabled, the string "1" is returned if enabled.

You might think that these values are evil, without any meaning, it would be totally stupid to enter such values yeah? The probability it might be raised if fairly low, let's just use the buggy obvious way.

Actually these are really logical when drunk. When you realize your sysadmins are often drunk, or that they simply prefer to enter "On" instead of "1" as it's easier to understand, you might have to change your strategy.