is_bool

Checks whether a variable is of type bool, without any truthiness conversion.

Signature
is_bool(mixed $value): bool
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$valuemixedThe variable being evaluated.

Return value

booltrue if $value is a bool, false otherwise.

is_bool() tests the type of a value, not whether it is truthy. It returns true for exactly two values, true and false, and false for everything else — including 0, 1, '', '0', 'true', null and empty arrays, all of which people expect to be "boolean-ish".

When to reach for it

Two situations make it genuinely useful.

Telling false apart from "nothing". Many PHP functions return a meaningful value or false on failure. strpos() is the classic: a match at position 0 and a failed search both look falsy, so you must test $pos === false or check is_bool($pos) rather than using if (!$pos).

Distinguishing an explicit setting from an absent one. In a config array, ['dark' => false] and a missing dark key mean different things. is_bool($config['dark'] ?? null) tells you the user actually chose something, which empty() cannot.

For validating input, remember that HTTP gives you strings, never bools. A form value of '1' or 'true' will always fail is_bool(); convert it first with filter_var($v, FILTER_VALIDATE_BOOL), then the type check is meaningful.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Distinguishing a genuine false return from strpos() or array_search() from a valid 0 index
  • Telling an explicitly disabled config option apart from one the user never set
  • Asserting that a callback or JSON field really returned a bool before storing it in a typed property

Frequently asked questions

Why does is_bool(0) return false?

Because is_bool() checks the type, not truthiness, and 0 is an int. Only the literal values true and false are of type bool.

What is the difference between is_bool() and boolval()?

is_bool() asks whether a value already is a bool and returns a yes/no answer; boolval() converts any value into a bool. They are a test and a conversion, not alternatives.

How do I check whether a form field is a boolean?

You cannot with is_bool() — form fields arrive as strings. Convert first with filter_var($v, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE), which yields true, false, or null when the string is not recognisable.

More examples

The strpos() zero trap

The match at index 0 is an int, so is_bool() is false for it while the failed search is a real bool. The last two lines show why loose comparison breaks here: 0 == false is true, but 0 === false is false.

Example · php
<?php
$miss = strpos('soundscode', 'z');
$hit  = strpos('soundscode', 's');

var_dump($miss, is_bool($miss));
var_dump($hit, is_bool($hit));

var_dump($hit == false);
var_dump($hit === false);

Explicitly set versus never set

'dark' was deliberately turned off and is reported as such, while 'beta' (null) and the absent 'missing' key both fall through to the default. empty() would have lumped all three together.

Example · php
<?php
$settings = ['dark' => false, 'beta' => null];

foreach (['dark', 'beta', 'missing'] as $key) {
    $v = $settings[$key] ?? null;
    printf("%-8s %s\n", $key,
        is_bool($v) ? 'explicitly ' . var_export($v, true) : 'not configured'
    );
}

Form input is never a bool

Every raw value fails is_bool() because HTTP delivers strings. After filter_var() the values are real bools — including 'off' becoming false, which a plain (bool) cast would have turned into true.

Example · php
<?php
$posted = ['accept' => '1', 'news' => 'true', 'ads' => 'off'];

foreach ($posted as $k => $raw) {
    $clean = filter_var($raw, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE);
    printf("%-7s raw is_bool=%-5s clean=%-5s is_bool=%s\n",
        $k,
        var_export(is_bool($raw), true),
        var_export($clean, true),
        var_export(is_bool($clean), true)
    );
}

Discussion

  • Be the first to comment on this lesson.