Re: 'false' - boolean interpratation
| From: | Hans Lellelid | Date: | Tue, 08 Jun 2004 18:06:58 +0000 |
| Subject: | Re: 'false' - boolean interpratation | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-10327@lists.php.net to get a copy of this message | ||
Robert Janeczek wrote:
is there any way that string 'false' (and its variations with capital letters) could be interpeted as boolean of value false? i have xml configuration file that has some switches (true/false) in it. when i use this values in functions that require booleans i get true every time (as string isn`t empty) :/ i hoped that at least with explicit type cast i could go around this problem, but i didn`t help. how about adding this special case to conversion rules? creating if`s around such code doesn`t make me happy at all :]IMO better as userland function/method (this is extract from a Phing class):
...
private static $TRUE_VALUES = array("on", "true", "t", "yes");
...
public function booleanValue($s) {
if (is_bool($s)) {
return $s; // it's already boolean (not a string)
}
// otherwise assume it's something like "true" or "t"
$trimmed = strtolower(trim($s));
return in_array($trimmed, self::$TRUE_VALUES);
}
Hans