Accueil / Blog / Métier / Archives / How to retrieve boolean values from PHP's configuration

How to retrieve boolean values from PHP's configuration

Par Emmanuelle Helly publié 24/06/2010

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.
  • If your setting is defined somewhere else, let's say in your httpd.conf, the exact string is returned.

As a result, a call to ini_get("safe_mode") may return any of the following if safe_mode is enabled :

"true", "truE", "trUe", "trUE", "tRue", "tRuE", "tRUe", "tRUE", "True", "TruE", "TrUe", "TrUE", "TRue", "TRuE", "TRUe", "TRUE", "yes", "yeS", "yEs", "yES", "Yes", "YeS", "YEs", "YES", "on", "oN", "On", "ON", "1", "2", "3", "42", ...

If safe_mode is disabled, any of the following :

"false", "falsE", "falSe", "falSE", "faLse", "faLsE", "faLSe", "faLSE", "fAlse", "fAlsE", "fAlSe", "fAlSE", "fALse", "fALsE", "fALSe", "fALSE", "False", "FalsE", "FalSe", "FalSE", "FaLse", "FaLsE", "FaLSe", "FaLSE", "FAlse", "FAlsE", "FAlSe", "FAlSE", "FALse", "FALsE", "FALSe", "FALSE", "no", "nO", "No", "NO", "off", "ofF", "oFf", "oFF", "Off", "OfF", "OFf", "OFF", "", "0", "HEY I CAN PUT STRING AND IT'S FALSE", "REALLY?!", "OH SHIT YEAH, COOL IDEA", ...

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.

The right but not so obvious way

That's quite a huge mess, but here is a better solution :

function ini_get_boolean($setting)
$my_boolean = ini_get($setting);

if ( (int) $my_boolean > 0 )
$my_boolean = true;
$my_lowered_boolean = strtolower($my_boolean);

if ($my_lowered_boolean === "true" || $my_lowered_boolean === "on" || $my_lowered_boolean === "yes")
$my_boolean = true;
$my_boolean = false;

return $my_boolean;

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