PHP is_bool() Function
Quick summary: The PHP is_bool() function checks whether a variable is of type boolean.
PHP is_bool() Syntax
is_bool(mixed $value): bool
PHP
PHP is_bool() Basic examples
var_dump(is_bool(true));
PHP
Output:
bool(true)
Returns true for boolean values.
var_dump(is_bool(0));
PHP
Output:
bool(false)
Integers are not booleans.
PHP is_bool() Real-world usage
$flag = false;
if (is_bool($flag)) {
echo 'Boolean flag';
}
PHP
Output:
Boolean flag
Validates strict boolean flags.
PHP is_bool() Edge cases
var_dump(is_bool('true'));
PHP
Output:
bool(false)
Strings are not booleans.
PHP is_bool() Common mistakes
Confusing truthy values with booleans
Not all truthy values are booleans.
Incorrect
is_bool(1);
Correct
is_bool(true);
Use strict booleans when type matters.
PHP is_bool() Frequently Asked Questions
What does is_bool() do?
Checks if a variable is boolean.
Return type?
bool.
Use case?
Type validation.
Common mistake?
Confusing truthy values.
Handles strings?
No.
Strict?
Yes.
Alternative?
gettype().
Performance?
Fast.
Safe?
Yes.
Used in APIs?
Yes.
Handles null?
No.
Best practice?
Validate input types.