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:11
Avg. Score:4.2 ± 0.7
Reproduced:10 of 10 (100.0%)
Same Version:6 (60.0%)
Same OS:8 (80.0%)
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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
25 - 14 = ?
Subscribe to this entry?

 
 [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

History

AllCommentsChangesGit/SVN commitsRelated reports
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 10:01:26 2024 UTC