Definition and Usage
- PHP Version
- 4+
The is_bool() function checks if a given value is of the boolean type.
It returns true if the value is boolean, and false otherwise.
The boolean type represents a data type that can only have one of two values: true (true) or false (false).
In PHP, although integers and strings can be evaluated as booleans in conditional expressions, the boolean type itself only has the two values, true and false.
Basic Example
// Boolean type
var_dump(is_bool(true)); // bool(true)
var_dump(is_bool(false)); // bool(true)
// Numbers
var_dump(is_bool(1)); // bool(false)
var_dump(is_bool(0)); // bool(false)
// Strings
var_dump(is_bool('hello')); // bool(false)
var_dump(is_bool('true')); // bool(false)
var_dump(is_bool('false')); // bool(false)
// The result of comparison operators (e.g., ===, !==) is always a boolean value
var_dump(is_bool(1 === 1)); // bool(true)
var_dump(is_bool(1 !== 1)); // bool(true)
Note:
The gettype() function returns the data type of the given value as a string.
Syntax
is_bool(mixed $value): bool
Parameters
$value |
Required. The value to be checked for boolean type. |
|---|
Return Values
The function returns true if the passed value is of boolean type, and false otherwise.
Things to Keep in Mind
The is_bool() function returns true only when the value exactly matches the boolean type. Therefore, in the following examples, each call returns false:
$a = null;
$b = '';
$c = 0;
var_dump(is_bool($a)); // bool(false)
var_dump(is_bool($b)); // bool(false)
var_dump(is_bool($c)); // bool(false)
Practical Examples
The is_bool() function is convenient and useful in the following situations:
When Checking if a Variable is of Boolean Type
When you need to check if a variable is of boolean type, you can use the is_bool() function to write more concise code. For example, the following code checks if a variable is of boolean type and performs different actions accordingly:
$a = true;
if (is_bool($a)) {
echo 'Variable $a is of boolean type.';
} else {
echo 'Variable $a is not of boolean type.';
}
// Output: 'Variable $a is of boolean type.'
When Using in Conditional Statements
When you need to check if a variable is of boolean type in a conditional statement, the is_bool() function allows you to write clear code. For example, the following code only performs a specific action if the variable is true:
$a = true;
if (is_bool($a) && $a === true) {
echo 'Variable $a is TRUE.';
} else {
echo 'Variable $a is not TRUE.';
}
// Output: 'Variable $a is TRUE.'
When Using as a Function Argument
If a function argument should be of boolean type, you can use the is_bool() function to ensure the variable is of the correct type. For example, the following code uses is_bool() to check the type before passing the variable as an argument to a function:
function my_function($bool) {
if ($bool === true) {
echo 'The variable is TRUE.';
} else {
echo 'The variable is not TRUE.';
}
}
$a = 1;
my_function(is_bool($a)); // Output: 'The variable is not TRUE.'