note 94951 deleted from function.is-bool by danbrown

From: Date: Fri, 04 Dec 2009 14:57:13 +0000
Subject: note 94951 deleted from function.is-bool by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163575@lists.php.net to get a copy of this message
Note Submitter: sam+nospam at samuellevy dot com ---- 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; } ?>

« previous php.notes (#163575) next »