W3docs

is_bool()

The is_bool() function is a built-in function in PHP that checks whether a variable is a boolean or not. A boolean is a data type that can have one of two

Introduction

is_bool() is a built-in PHP function that reports whether a variable holds a boolean value. A boolean is the simplest data type in PHP: it can only be true or false.

The key thing to understand is that is_bool() checks the variable's type, not whether the value is "truthy" or "falsy". A string like "true", the integer 1, or an empty array all evaluate as truthy or falsy in an if statement, but none of them are actually booleans, so is_bool() returns false for all of them. Only a value that came from a boolean literal (true/false) or a comparison/logical expression (which always produces a boolean) passes the test.

This page covers the syntax, what is_bool() returns for the values you are likely to test, and when you would reach for it in real code.

Syntax

is_bool(mixed $value): bool

It takes a single argument, $value — the variable you want to inspect — and returns true if that value is of type boolean, false otherwise. The function never modifies the variable and never raises an error: it always returns a boolean of its own.

Example usage

The example below tests a true boolean against a string so you can see exactly what gets printed:

Example of PHP is_bool()

php— editable, runs on the server

Note the output carefully: when you echo a boolean, true becomes the string "1" and false becomes an empty string "". That is why the second line looks blank — is_bool($name) returned false, and PHP prints false as nothing. To see an unambiguous result during debugging, use var_dump() instead of echo, because it prints bool(true) / bool(false).

What counts as a boolean

A common mistake is assuming that "truthy" values are booleans. They are not. The table below shows what is_bool() returns for a range of values:

<?php
var_dump(is_bool(true));        // bool(true)
var_dump(is_bool(false));       // bool(true)
var_dump(is_bool(5 > 3));       // bool(true)  — a comparison yields a boolean
var_dump(is_bool(1));           // bool(false) — integer, not boolean
var_dump(is_bool("true"));      // bool(false) — string, not boolean
var_dump(is_bool(null));        // bool(false) — null is its own type
var_dump(is_bool([]));          // bool(false) — empty array
?>

The values 1, "true", null, and [] are all falsy or truthy in conditions, but their type is integer, string, null, and array respectively — so is_bool() rejects them.

When to use it

is_bool() is most useful at the boundaries of your code, where data type matters:

  • Validating function arguments. If a function expects a strict on/off flag, is_bool() lets you reject "yes", 1, or null before they cause subtle bugs.
  • Distinguishing false from "no value". Functions such as strpos() return false on failure but a valid 0 on success. Checking the type with is_bool() (or the === operator) avoids confusing the two.
  • Handling mixed configuration. Settings loaded from JSON or a form may arrive as strings; is_bool() tells you whether a value is already a real boolean or still needs conversion with boolval().
<?php
function setFeature(string $name, $enabled): void
{
    if (!is_bool($enabled)) {
        throw new InvalidArgumentException("\$enabled must be a boolean.");
    }
    echo "$name is now " . ($enabled ? "on" : "off") . "\n";
}

setFeature("dark_mode", true);   // dark_mode is now on
// setFeature("dark_mode", 1);   // would throw: $enabled must be a boolean
?>

is_bool() is one of PHP's family of type-checking functions. The right one depends on the type you care about:

Conclusion

is_bool() answers one precise question: is this variable's type boolean? It returns true only for true, false, and the results of comparison or logical expressions — never for truthy strings, numbers, or arrays. Use it to guard function arguments, tell a real false apart from a "missing" value, and keep your code working with the exact data types you expect.

Practice

Practice
What does the is_bool() function in PHP do?
What does the is_bool() function in PHP do?
Was this page helpful?