note 98410 added to language.types.boolean

From: Date: Mon, 14 Jun 2010 11:39:55 +0000
Subject: note 98410 added to language.types.boolean
Groups: php.notes 
Request: Send a blank email to php-notes+get-170109@lists.php.net to get a copy of this message
Since I haven't seen it posted. Here is a function that you can use if you have a need to force strict boolean values. Hopefully this will save someone some time from searching for similar. <?php function strictBool($val=false){ return is_integer($val)?false:$val == 1; } ?> Simply put, it verifies that the value passed is (bool)true otherwise it's false. Examples: __________________________________ <?php $myBool = strictBool(true); var_dump($myBool); //returns (bool)true $myar = array(0 => true); $myBool = strictBool($myar[0]); var_dump($myBool); //returns (bool)true $myBool = strictBool("hello"); var_dump($myBool); //returns (bool)false $myBool = strictBool(false); var_dump($myBool); //returns (bool)false $myBool = strictBool(array(0 => "hello")); var_dump($myBool); //returns (bool)false $myBool = strictBool(1); var_dump($myBool); //returns (bool)false $myBool = strictBool(); var_dump($myBool); //returns (bool)false ?> ---- Server IP: 69.147.83.197 Probable Submitter: 68.170.225.93 ---- Manual Page -- http://www.php.net/manual/en/language.types.boolean.php Edit -- https://master.php.net/note/edit/98410 Del: integrated -- https://master.php.net/note/delete/98410/integrated Del: useless -- https://master.php.net/note/delete/98410/useless Del: bad code -- https://master.php.net/note/delete/98410/bad+code Del: spam -- https://master.php.net/note/delete/98410/spam Del: non-english -- https://master.php.net/note/delete/98410/non-english Del: in docs -- https://master.php.net/note/delete/98410/in+docs Del: other reasons-- https://master.php.net/note/delete/98410 Reject -- https://master.php.net/note/reject/98410 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170109) next »