Types7 min readExamples run on PHP 8.5.10

is_bool()

Learn how PHP 8.4 is_bool() tests for a real true or false value, why 0, empty strings and null do not count, and how to avoid common type-check mistakes.

Signature

is_bool(mixed $value): bool

Purpose

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

  • $value is 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.

Example
<?php
$values = [true, false, 0, 1, "true", "", null, [], 0.0];
$results = [];
foreach ($values as $value) {
    $results[] = is_bool($value);
}
echo json_encode($results), "\n";
Output
[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.

Example
<?php
$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";
Output
{"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.

Example
<?php
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";
Output
["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

  • null is not a boolean, even though it is falsy.
  • 0, 0.0, "0" and "" are not booleans. Neither are 1 or "1".
  • An empty array is falsy but its type is array, so the result is false.
  • Boolean values cast from other types, such as (bool) 0, are real booleans and pass the check.
  • Results of comparisons like 5 > 3 or of in_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. Use filter_var() for that job.

Common mistakes

  1. Using truthiness as a type check. if ($value) accepts any non-empty value, while is_bool($value) accepts only real booleans.
  2. Expecting numeric strings to qualify. Form fields and environment-style values usually arrive as strings, so is_bool() will report false until you convert them.
  3. Treating false from a function as an error without checking. Some functions return false on failure while others return null. Combine === false or is_bool() with knowledge of the specific function.
  4. Confusing the check with conversion. is_bool() never changes the value. If you want conversion, use a cast or boolval() and be aware that it follows PHP's truthiness rules.
  5. Forgetting the result is itself a boolean. Writing is_bool($x) === true works, but is_bool($x) alone is clearer.
  • is_int(), is_float() and is_string() test the other scalar types.
  • is_array() and is_object() test compound types.
  • gettype() returns the type name as a string.
  • filter_var() with FILTER_VALIDATE_BOOLEAN interprets 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

Which values are booleans
<?php
$values = [true, false, 0, 1, "true", "", null, [], 0.0];
$results = [];
foreach ($values as $value) {
    $results[] = is_bool($value);
}
echo json_encode($results), "\n";
Output
[true,true,false,false,false,false,false,false,false]
Distinguishing parsed flags from failed parses
<?php
$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";
Output
{"yes":true,"off":true,"maybe":false}
Validating configuration values
<?php
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";
Output
["bool","not bool","not bool","missing"]