note 88635 added to function.is-bool

From: Date: Mon, 02 Feb 2009 00:30:00 +0000
Subject: note 88635 added to function.is-bool
Groups: php.notes 
Request: Send a blank email to php-notes+get-150079@lists.php.net to get a copy of this message
Just another little function which doesn't exist yet, but I find mighty useful, especially when working with AJAX and APIs. <?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 */ function boolval($in, $strict=false) { $out = null; // if not strict, we only have to check if something is false if (in_array($in,array('false', 'False', 'FALSE', 'no', 'No', 'n', 'N', '0', 'off', 'Off', 'OFF', false, 0, null), true)) { $out = false; } else if ($strict) { // if strict, check the equivalent true values if (in_array($in,array('true', 'True', 'TRUE', 'yes', 'Yes', 'y', 'Y', '1', 'on', 'On', 'ON', true, 1), 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; } ?> It may be pretty inefficient, but it does the job. ---- Server IP: 117.55.229.1 Probable Submitter: 203.206.187.200 ---- Manual Page -- http://www.php.net/manual/en/function.is-bool.php Edit -- https://master.php.net/note/edit/88635 Del: integrated -- https://master.php.net/note/delete/88635/integrated Del: useless -- https://master.php.net/note/delete/88635/useless Del: bad code -- https://master.php.net/note/delete/88635/bad+code Del: spam -- https://master.php.net/note/delete/88635/spam Del: non-english -- https://master.php.net/note/delete/88635/non-english Del: in docs -- https://master.php.net/note/delete/88635/in+docs Del: other reasons-- https://master.php.net/note/delete/88635 Reject -- https://master.php.net/note/reject/88635 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150079) next »