How to check if a variable is a Boolean in PHP?
Boolean variables contain true or false values. To find out if any variable contains a Boolean value, different approaches can be used:
- is_bool($var): The function is_bool() checks whether a variable’s type is Boolean and returns true or false.
- Comparison using “===” or ($var===true || $var===false): If you compare the variable with the operator “===” with true and false and if it is identical to one of the values, then it is a boolean value. Since the functionality of is_bool($var) is same, the function should be preferred.
- Comparison using “==” or ($var == true || $var == false): If you use “==” as an operator instead, the test is more tolerant. For example, 0(integer) or “”(empty string) would also result in a Boolean value.
- gettype($var): If you apply gettype() to a variable, you will get the datatype of the variable as a string. This can be evaluated. In case of boolean variables it must have the value “boolean”.
The recommended way to check the variables is to use is_bool(). It is readable for other developers and is performant at the same time.
Example 1: is_bool($var)
<?php function myFunction($bool) { if (!is_bool($bool)) { throw new Exception('Function myFunction expects a Boolean value.'); } echo("myFunction called successfully.\n"); } try { myFunction(false); myFunction(true); myFunction(0); } catch (Exception $e) { echo('Error with message "'.$e->getMessage().'" in line '.$e->getLine().'.'); } ?>
Output:
myFunction called successfully. myFunction called successfully. Error with message "Function myFunction expects a Boolean value." in line 6.
Example 2: Comparison using “===”
<?php function myFunction($bool) { if ($bool!==true && $bool!==false) { throw new Exception('Function myFunction expects a Boolean value.'); } echo("myFunction called successfully.\n"); } try { myFunction(false); myFunction(true); myFunction(0); } catch (Exception $e) { echo('Error with message "'.$e->getMessage().'" in line '.$e->getLine().'.'); } ?>
Output:
myFunction called successfully. myFunction called successfully. Error with message "Function myFunction expects a Boolean value." in line 6.
Example 3: Comparison using “==”
<?php function myFunction($bool) { if ($bool!=true && $bool!=false) { throw new Exception('Function myFunction expects a Boolean value.'); } echo("myFunction called successfully.\n"); } try { myFunction(false); myFunction(true); myFunction(0); } catch (Exception $e) { echo('Error with message "'.$e->getMessage().'" in line '.$e->getLine().'.'); } ?>
Output:
myFunction called successfully. myFunction called successfully. myFunction called successfully.
Example 4: Using gettype()
<?php function myFunction($bool) { if (gettype($bool) !== 'boolean') { throw new Exception('Function myFunction expects a Boolean value.'); } echo("myFunction called successfully.\n"); } try { myFunction(false); myFunction(true); myFunction(0); } catch (Exception $e) { echo('Error with message "'.$e->getMessage().'" in line '.$e->getLine().'.'); } ?>
Output:
myFunction called successfully. myFunction called successfully. Error with message "Function myFunction expects a Boolean value." in line 6.