note 94951 added to function.is-bool
| From: | sam+nospam at samuellevy dot com | Date: | Fri, 04 Dec 2009 01:06:58 +0000 |
| Subject: | note 94951 added to function.is-bool | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163561@lists.php.net to get a copy of this message | ||
OK, just a little update to mine to make it more robust. I believe that it's reasonable
elegant, and seems to work a treat.
<?php
/** Checks a variable to see if it should be considered a boolean true or false.
* Also takes into account some text-based representations of true of false,
* such as 'false','N','yes','on','off', etc.
* @author Samuel Levy <sam+nospam@samuellevy.com>
* @param mixed $in The variable to check
* @param bool $strict If set to false, consider everything that is not false to
* be true.
* @return bool The boolean equivalent or null (if strict, and no exact equivalent)
*/
function boolval($in, $strict=false) {
$out = null;
$in = (is_string($in)?strtolower($in):$in);
// if not strict, we only have to check if something is false
if (in_array($in,array('false','no',
'n','0','off',false,0), true) || !$in) {
$out = false;
} else if ($strict) {
// if strict, check the equivalent true values
if
(in_array($in,array('true','yes','y','1','on',true,
), true)) {
$out = true;
}
} else {
// not strict? let the regular php bool check figure it out (will
// largely default to true)
$out = ($in?true:false);
}
return $out;
}
?>
----
Server IP: 117.104.162.229
Probable Submitter: 203.206.187.200 (proxied: 203.206.187.200)
----
Manual Page -- http://www.php.net/manual/en/function.is-bool.php
Edit -- https://master.php.net/note/edit/94951
Del: integrated -- https://master.php.net/note/delete/94951/integrated
Del: useless -- https://master.php.net/note/delete/94951/useless
Del: bad code -- https://master.php.net/note/delete/94951/bad+code
Del: spam -- https://master.php.net/note/delete/94951/spam
Del: non-english -- https://master.php.net/note/delete/94951/non-english
Del: in docs -- https://master.php.net/note/delete/94951/in+docs
Del: other reasons-- https://master.php.net/note/delete/94951
Reject -- https://master.php.net/note/reject/94951
Search -- https://master.php.net/manage/user-notes.php