ZetCode

PHP bool Data Type

last modified November 25, 2026

The PHP bool type represents a truth value: true or false. Booleans come out of comparisons and logical operations, and they decide which branch of a program runs.

This article shows how booleans are declared, how conditions use them, which values convert to false, how loose and strict comparisons differ, and how boolean types behave in function signatures.

Basic definitions

The bool type holds one of two values: true or false. The two constants are case-insensitive, so TRUE is accepted, but lowercase is the ordinary style.

A boolean usually appears as the result of a comparison or a logical operation, and it controls the flow of if, while, and the ternary operator. Many built-in functions also return a boolean to report success, such as in_array() and file_exists().

Where a boolean is expected, PHP also accepts other values and converts them. The false values are false, 0, 0.0, an empty string, the string "0", an empty array, and null. Everything else is true, including "0.0", "false", -1, and a string containing only a space. The full table is in PHP type conversions.

With declare(strict_types=1), a declared bool parameter or return type stops accepting other types. Without strict types, an integer or a string is converted, which can hide a mistake.

Declaring booleans

The first example declares two boolean variables and prints their type and value.

bool_declare.php
<?php

declare(strict_types=1);

$isActive = true;
$isAdmin = false;

var_dump($isActive);
var_dump($isAdmin);
echo $isActive ? "active\n" : "inactive\n";

var_dump() shows bool(true) and bool(false), so the type is visible as well as the value. The ternary in the last line uses the flag directly as the condition; there is no need to compare it with true. Booleans are often named after the question they answer, such as $isActive or $hasPermission.

$ php bool_declare.php
bool(true)
bool(false)
active

Booleans in conditions

An if statement evaluates its expression in boolean context. A boolean variable can be used as the whole condition.

bool_condition.php
<?php

declare(strict_types=1);

$hasPermission = true;

if ($hasPermission) {
    echo "Access granted\n";
} else {
    echo "Access denied\n";
}

$hasPermission = false;

if (!$hasPermission) {
    echo "Request rejected\n";
}

The first branch runs because $hasPermission is true. After the value is set to false, the negated condition !$hasPermission becomes true and the second message is printed. Writing if ($hasPermission === true) would work too, but the comparison adds nothing when the variable already holds a boolean.

$ php bool_condition.php
Access granted
Request rejected

Casting and truthiness

The (bool) cast makes the conversion explicit. The next example shows the cases that surprise readers most.

bool_cast.php
<?php

declare(strict_types=1);

var_dump((bool) "");
var_dump((bool) "0");
var_dump((bool) "0.0");
var_dump((bool) "false");
var_dump((bool) 0);
var_dump((bool) 0.0);
var_dump((bool) []);

An empty string and the integer zero are false, as expected, and the empty array is false as well. The interesting lines are the strings: "0" is the only non-empty string that converts to false, while "0.0" and "false" are true because they are ordinary non-empty text. This special case is why a numeric string from a form should be tested with a comparison rather than with if ($value).

$ php bool_cast.php
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)

Loose and strict comparisons

Comparison operators always produce a boolean, but the two families of operators compare differently.

bool_compare.php
<?php

declare(strict_types=1);

var_dump(1 == true);
var_dump(1 === true);
var_dump("1" == "01");
var_dump("1" === "01");

$stock = 0;

if ($stock == false) {
    echo "No stock\n";
}

== converts the operands to a common type before comparing them, so 1 == true is true and the numeric strings "1" and "01" are equal. === also compares the types, which makes both checks false. The same conversion is why $stock == false succeeds when the stock is 0. Use === for booleans and for strings; PHP string equality covers the string cases in detail.

$ php bool_compare.php
bool(true)
bool(false)
bool(true)
bool(false)
No stock

A bool return type

Declaring bool as the return type documents the answer and lets the engine check it.

bool_function.php
<?php

declare(strict_types=1);

function isAdult(int $age): bool
{
    return $age >= 18;
}

$ages = [15, 18, 42];

foreach ($ages as $age) {
    echo $age, ": ", isAdult($age) ? "adult" : "minor", "\n";
}

isAdult() returns the result of a comparison, which is already a boolean. A function whose name starts with is is expected to return a boolean, and the declared type makes that expectation enforceable. If the function later returns an integer by mistake, PHP reports a TypeError instead of letting the wrong value travel through the program.

$ php bool_function.php
15: minor
18: adult
42: adult

A bool parameter under strict types

The same check applies to parameters. The next example passes an integer to a boolean parameter on purpose.

bool_param.php
<?php

declare(strict_types=1);

function setEnabled(bool $enabled): void
{
    echo $enabled ? "enabled\n" : "disabled\n";
}

setEnabled(true);

try {
    setEnabled(1);
} catch (TypeError $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
}

Under strict_types=1, the call raises TypeError: setEnabled(): Argument #1 ($enabled) must be of type bool, int given. Without strict types the integer 1 would be converted to true and the call would succeed, which is exactly the kind of silent conversion the declaration is meant to prevent. Pass an actual comparison or a boolean variable instead.

$ php bool_param.php
enabled
TypeError: setEnabled(): Argument #1 ($enabled) must be of type bool, int given, called in /app/bool_param.php on line 13

Logical operators and short-circuiting

&&, ||, and ! combine booleans. The first two stop evaluating as soon as the result is known.

bool_logic.php
<?php

declare(strict_types=1);

function check(): bool
{
    echo "check() called\n";

    return true;
}

var_dump(false && check());
var_dump(true || check());

$loggedIn = true;
$isAdmin = false;

var_dump($loggedIn && $isAdmin);
var_dump(!$isAdmin);

The output contains no check() called line: false && check() stops at the first operand, and true || check() stops at the first as well. This short-circuiting is why $user !== null && $user->isActive() is safe. The last two lines show the ordinary combination of two flags and a negation. The keyword forms and and or exist too, but they bind less tightly than =, as described in PHP pitfalls.

$ php bool_logic.php
bool(false)
bool(true)
bool(false)
bool(true)

Best practices

Treat a boolean as a fact with a name, and keep the type honest.

The PHP type conversions tutorial has the complete conversion table, and PHP string equality explains the difference between == and === for strings.

Source

PHP manual: booleans

PHP manual: type juggling

This article covered the PHP bool type: declaration, conditions, casting and truthiness, comparisons, return and parameter types, and logical operators. Continue with PHP int keyword for integers, or read PHP type conversions for the full set of conversions.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.