Signature
is_bool(mixed $value): boolPurpose
is_bool() answers a narrow question: is the value you hold an actual PHP boolean, meaning exactly true or false? It looks only at the type of the value. It does not ask whether the value is truthy, falsy, empty or similar to a flag. This makes it useful when a value arrives from configuration, decoded JSON, a parser or a loosely typed array and you must be sure it is a real boolean before you rely on strict comparisons.
Signature
The function is available in PHP 8.4 and has existed since PHP 4.
Parameters and defaults
$valueis the only parameter. It accepts any type (mixed) and has no default, so you must always pass an argument.
Because the parameter is mixed, passing an integer, string, array, object or null never triggers a type error. Those values simply produce false.
Return value and failure cases
is_bool() returns true when the value is of type bool, and false for everything else. It does not throw exceptions and has no special error return. The one situation that produces a diagnostic is reading an undefined variable as the argument: PHP raises its usual undefined variable warning while evaluating the argument, and the function then receives null and returns false.
Example 1: what counts as a boolean
The first example runs a mixed list through the function. Only the two real booleans pass. Numbers, strings, null and arrays that are merely falsy or truthy do not.
$values = [true, false, 0, 1, "true", "", null, [], 0.0];
$results = [];
foreach ($values as $value) {
$results[] = is_bool($value);
}
echo json_encode($results), "\n";[true,true,false,false,false,false,false,false,false]
Example 2: telling parsed flags from failed parses
filter_var() with FILTER_VALIDATE_BOOLEAN and FILTER_NULL_ON_FAILURE returns true or false for recognized words and null for anything else. Combining it with is_bool() lets you tell a valid false apart from a failed parse. A plain truthiness test cannot do that, because both look falsy.
$inputs = ["yes", "off", "maybe"];
$report = [];
foreach ($inputs as $input) {
$parsed = filter_var($input, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
$report[$input] = is_bool($parsed);
}
echo json_encode($report), "\n";{"yes":true,"off":true,"maybe":false}
Both "yes" and "off" were understood, so each yields a boolean. "maybe" was not understood, so the parse gives null and the check is false.
Example 3: validating configuration values
Settings often come from loosely typed sources. A string such as "false" is truthy in PHP, which is a classic source of bugs. Checking the type first exposes the problem.
function readFlag(array $config, string $key): string
{
if (!array_key_exists($key, $config)) {
return "missing";
}
return is_bool($config[$key]) ? "bool" : "not bool";
}
$config = ["debug" => false, "cache" => "false", "retries" => 0];
echo json_encode([
readFlag($config, "debug"),
readFlag($config, "cache"),
readFlag($config, "retries"),
readFlag($config, "other"),
]), "\n";["bool","not bool","not bool","missing"]
The helper uses array_key_exists() first, because a missing key and a key holding null are different situations.
Edge cases
nullis not a boolean, even though it is falsy.0,0.0,"0"and""are not booleans. Neither are1or"1".- An empty array is falsy but its type is
array, so the result isfalse. - Boolean values cast from other types, such as
(bool) 0, are real booleans and pass the check. - Results of comparisons like
5 > 3or ofin_array()are booleans, so they pass. - The function inspects only the type, so it gives no hint about whether a string such as
"on"could be interpreted as a flag. Usefilter_var()for that job.
Common mistakes
- Using truthiness as a type check.
if ($value)accepts any non-empty value, whileis_bool($value)accepts only real booleans. - Expecting numeric strings to qualify. Form fields and environment-style values usually arrive as strings, so
is_bool()will reportfalseuntil you convert them. - Treating
falsefrom a function as an error without checking. Some functions returnfalseon failure while others returnnull. Combine=== falseoris_bool()with knowledge of the specific function. - Confusing the check with conversion.
is_bool()never changes the value. If you want conversion, use a cast orboolval()and be aware that it follows PHP's truthiness rules. - Forgetting the result is itself a boolean. Writing
is_bool($x) === trueworks, butis_bool($x)alone is clearer.
Related functions
is_int(),is_float()andis_string()test the other scalar types.is_array()andis_object()test compound types.gettype()returns the type name as a string.filter_var()withFILTER_VALIDATE_BOOLEANinterprets flag-like text.boolval()converts a value to a boolean using PHP's conversion rules.
Recap
is_bool() takes one value of any type and returns true only for the real booleans true and false. It never throws, never converts, and treats null, numbers, strings and arrays as non-booleans no matter how falsy they are. Use it when the exact type matters, and pair it with filter_var() when you need to interpret text as a flag.
Tested examples
$values = [true, false, 0, 1, "true", "", null, [], 0.0];
$results = [];
foreach ($values as $value) {
$results[] = is_bool($value);
}
echo json_encode($results), "\n";
[true,true,false,false,false,false,false,false,false]
$inputs = ["yes", "off", "maybe"];
$report = [];
foreach ($inputs as $input) {
$parsed = filter_var($input, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
$report[$input] = is_bool($parsed);
}
echo json_encode($report), "\n";
{"yes":true,"off":true,"maybe":false}
function readFlag(array $config, string $key): string
{
if (!array_key_exists($key, $config)) {
return "missing";
}
return is_bool($config[$key]) ? "bool" : "not bool";
}
$config = ["debug" => false, "cache" => "false", "retries" => 0];
echo json_encode([
readFlag($config, "debug"),
readFlag($config, "cache"),
readFlag($config, "retries"),
readFlag($config, "other"),
]), "\n";
["bool","not bool","not bool","missing"]