What is the is_bool method in PHP?
The is_bool method can be used to check if the variable is a Boolean.
Syntax
is_bool(mixed $value): bool
This method returns true if the passed value is a Boolean. Otherwise, it returns false.
Code
<?php$test = true;printIsBoolean($test);$test = false;printIsBoolean($test);$test = 0;printIsBoolean($test);$test = 'true';printIsBoolean($test);function printIsBoolean($test){echo "The Value is: ";var_dump($test);echo "Is Boolean: ";var_dump(is_bool($test));echo "---------\n\n";}?>
Explanation
In the code above:
-
We have used the
is_boolmethod to check if the variable is aBoolean. -
The
is_boolmethod returnstruefor the valuetrueandfalse. -
The
is_boolmethod returnsfalsefor the value0(int)and'false'(string)because it is not aBooleantype variable.