php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52168 ini_get() better consistent behavior with booleans
Submitted: 2010-06-24 11:21 UTC Modified: -
Votes:10
Avg. Score:4.1 ± 0.7
Reproduced:9 of 9 (100.0%)
Same Version:5 (55.6%)
Same OS:7 (77.8%)
From: mxs at buffout dot org Assigned:
Status: Open Package: PHP options/info functions
PHP Version: Irrelevant OS: GNU/Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2010-06-24 11:21 UTC] mxs at buffout dot org
Description:
------------
The ini_get("boolean_setting") function may return several kind of value when dealing with boolean settings, as explained in the documentation :

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

The last part is a really annoying behavior, as to get the boolean value of a boolean setting, the following must be done :

$my_boolean = ini_get("boolean_setting");
if ($my_boolean === "0") {
   $my_boolean = false;
} else {
   $my_lowered_boolean = strtolower($my_boolean);
   if ($my_lowered_boolean === "false" || 
       $my_lowered_boolean === "off" ||
       $my_lowered_boolean === "no") {
      $my_boolean = false;
   } else {
      $my_boolean = true;
   }
}

I've noticed that the behavior is different when the setting has been set in php.ini, or in apache's configuration. Settings from php.ini are always set to "" or "1", whereas apache's settings (via php_value) are set to their litteral value (ie: "FalsE", "oFF", "off", ...). Lot of chances that the devs miss it!

When the setting doesn't exist, "" or null is returned. So that's quite a mess to deal with such values, and there is no way to test the existence of a setting.


Test script:
---------------
# Exemple 1

# httpd.conf
php_admin_value safe_mode               FaLsE
# php script
ini_get("safe_mode") === "FaLsE"

---

# Exemple 2

# php.ini
safe_mode = FaLsE
# php script
ini_get("safe_mode") === ""


Expected result:
----------------
It would be a great enhancement no to depend on what is written in configuration files, but to depend on the way it's being interpreted by the engine.

# Example 1

# httpd.conf
php_admin_value safe_mode               FaLsE
# php script
ini_get("safe_mode") === false

---

# Example 2

# php.ini
safe_mode = FaLsE
# php script
ini_get("safe_mode") === false

---

# Example 3
# Non existing setting
ini_get("unset_setting") === null

Actual result:
--------------
A disabled boolean setting may return one of the following:

ini_get("safe_mode") === "FaLsE"
ini_get("safe_mode") === "Off"
ini_get("safe_mode") === "oFF"
ini_get("safe_mode") === "0"
...

An enabled boolean setting may return one of the following:

ini_get("safe_mode") === "truE"
ini_get("safe_mode") === "On"
ini_get("safe_mode") === "oN"
ini_get("safe_mode") === "1"
...

An unset setting may return one of the following:

ini_get("safe_mode") === ""
ini_get("safe_mode") === null


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Nov 14 09:01:30 2019 UTC