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_bool method to check if the variable is a Boolean.

  • The is_bool method returns true for the value true and false.

  • The is_bool method returns false for the value 0(int) and 'false'(string) because it is not a Boolean type variable.

Free Resources