Definition and Usage
- PHP Version
- 4+
The is_bool() function checks whether a given value is a boolean.
It returns true if the value passed as an argument is a boolean; otherwise, it returns false.
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 check. |
|---|
Return Values
If the value passed as an argument is a boolean, the function returns true; otherwise, it returns false.
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.'
References
See also
- PHP gettype() Function – Get the Data Type of a Variable as a String
- PHP is_int() Function – Checking Whether a Value Is an Integer
- PHP is_float() Function – Checking Whether a Value Is a Float
- PHP is_numeric() Function – Checking Whether a Value Is a Number or a Numeric String
- PHP is_string() Function – Checking Whether a Value Is a String
- PHP is_array() Function – Checking Whether a Value Is an Array
- PHP is_object() Function – Checking Whether a Value Is an Object
- PHP is_resource() Function – Checking Whether a Value Is a Resource
- PHP is_null() Function – Checking Whether a Value Is NULL
- PHP ctype_digit() Function – Checking Whether a String Contains Only Numeric Character(s)
- PHP isset() and empty() Functions – Concepts, Usage, and Key Differences
- PHP Superglobals – Simple and Essential Usage Guide