Function to force strict boolean values in PHP

By: Fyrye Viewed: 153234 times  Printer Friendly Format    


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
?>


Most Viewed Articles (in PHP )

Latest Articles (in PHP)

Comment on this tutorial