Types7 min readExamples run on PHP 8.5.10

boolval()

Learn how boolval() converts any PHP value to true or false, which values count as falsy, and where string input like "false" surprises people.

Signature

boolval(mixed $value): bool
Available since PHP 5.5.07.48.08.18.28.38.48.5All versions →

Purpose

boolval() takes a value of any type and returns the boolean PHP would produce with a (bool) cast. It answers one question: would this value be treated as true or false in a condition? Because it is an ordinary function, you can pass it by name wherever PHP expects a callback, for example to array_filter() or array_map().

Signature

Parameters and defaults

There is a single required parameter, $value. It is typed mixed, so integers, floats, strings, arrays, objects and null are all accepted. There are no optional parameters and no defaults.

Return value and failure cases

The function always returns true or false. It does not throw for unusual input, and it emits no warnings for arrays or objects. The only way to get an error is to call it incorrectly, such as passing no argument at all, which raises an ArgumentCountError.

The following values convert to false:

  • the integer 0 and the floats 0.0 and -0.0
  • the empty string "" and the string "0"
  • an empty array
  • null

Almost everything else converts to true. That includes negative numbers, NAN, any non-empty array, and every object created from a normal class such as stdClass.

Example 1: scalars and strings

This loop prints the result for several numbers and strings. Pay attention to the strings: only "" and "0" are false. Text such as "0.0" or a single space is true, because PHP does not parse the string as a number here.

Example
<?php
$cases = [
    'int 0' => 0,
    'int 42' => 42,
    'float 0.0' => 0.0,
    'float 4.2' => 4.2,
    'empty string' => '',
    'string "string"' => 'string',
    'string "0"' => '0',
    'string "1"' => '1',
    'string "0.0"' => '0.0',
    'string " "' => ' ',
];
foreach ($cases as $label => $value) {
    echo $label, ': ', boolval($value) ? 'true' : 'false', "\n";
}
Output
int 0: false
int 42: true
float 0.0: false
float 4.2: true
empty string: false
string "string": true
string "0": false
string "1": true
string "0.0": true
string " ": true

Example 2: arrays, objects and special numbers

Compound values are judged by emptiness, not by their contents. An array holding a single null is still non-empty, so it is true. Objects are true even when they have no properties. Among floats, -0.0 is false, but NAN is true; PHP 8.5 also emits a warning when it converts NAN to bool.

Example
<?php
$results = [
    'empty array' => boolval([]),
    'array with null' => boolval([null]),
    'null' => boolval(null),
    'object' => boolval(new stdClass),
    'negative zero' => boolval(-0.0),
];
echo json_encode($results), "\n";
Output
{"empty array":false,"array with null":true,"null":false,"object":true,"negative zero":false}

Example 3: as a callback, and the "false" string trap

Passing 'boolval' to array_filter() keeps only truthy items. Note that array_filter() preserves keys, so array_values() is used to reindex the result. The second line shows a classic mistake: the string "false" is a non-empty string, so boolval() returns true. If you need to interpret textual flags such as "false", "off" or "no", use filter_var() with FILTER_VALIDATE_BOOLEAN instead.

Example
<?php
$mixed = [0, 1, '', 'a', '0', null, [], [0]];
echo json_encode(array_values(array_filter($mixed, 'boolval'))), "\n";
echo json_encode([boolval('false'), filter_var('false', FILTER_VALIDATE_BOOLEAN)]), "\n";
Output
[1,"a",[0]]
[true,false]

Edge cases

  • "0" is false but "00", "0.0" and " 0" are true, since only the exact one-character string "0" is special.
  • NAN is true even though it is not a usable number, and PHP 8.5 warns about the conversion. Test with is_nan() first.
  • -0.0 is false, the same as 0.0.
  • Arrays are judged only by whether they have elements, so [0] and [false] are both true.
  • Objects are true regardless of their state. Do not expect boolval() to ask an object whether it is "empty".

Common mistakes

  1. Treating "false" as false. Any non-empty string other than "0" is true. Parse user or configuration text deliberately, with filter_var() or an explicit comparison.
  2. Using boolval() to validate input. It only reports truthiness. It cannot tell you whether "abc" is a legitimate boolean setting.
  3. Expecting array_filter() to drop only null. With boolval as the callback (or with no callback), 0, "0", "" and [] are removed too. This can silently discard valid zero values.
  4. Assuming it mutates the variable. boolval() returns a new value and leaves its argument unchanged. To change the type of a variable in place, use settype().
  5. Confusing it with strict checks. boolval($x) being true does not mean $x === true. Use is_bool() or === when the exact type matters.
  • intval(), floatval() and strval() perform the equivalent conversions to other scalar types.
  • settype() converts a variable to a given type in place.
  • is_bool() checks whether a value is already a boolean.
  • filter_var() with FILTER_VALIDATE_BOOLEAN interprets textual flags.

Recap

boolval() mirrors a (bool) cast: it returns false for 0, 0.0, -0.0, "", "0", [] and null, and true for nearly everything else. It never throws for a supplied value, works well as a callback, and should not be used to interpret strings like "false".

Tested examples

Scalars and strings
<?php
$cases = [
    'int 0' => 0,
    'int 42' => 42,
    'float 0.0' => 0.0,
    'float 4.2' => 4.2,
    'empty string' => '',
    'string "string"' => 'string',
    'string "0"' => '0',
    'string "1"' => '1',
    'string "0.0"' => '0.0',
    'string " "' => ' ',
];
foreach ($cases as $label => $value) {
    echo $label, ': ', boolval($value) ? 'true' : 'false', "\n";
}
Output
int 0: false
int 42: true
float 0.0: false
float 4.2: true
empty string: false
string "string": true
string "0": false
string "1": true
string "0.0": true
string " ": true
Arrays, objects and special numbers
<?php
$results = [
    'empty array' => boolval([]),
    'array with null' => boolval([null]),
    'null' => boolval(null),
    'object' => boolval(new stdClass),
    'negative zero' => boolval(-0.0),
];
echo json_encode($results), "\n";
Output
{"empty array":false,"array with null":true,"null":false,"object":true,"negative zero":false}
Callback use and the "false" string trap
<?php
$mixed = [0, 1, '', 'a', '0', null, [], [0]];
echo json_encode(array_values(array_filter($mixed, 'boolval'))), "\n";
echo json_encode([boolval('false'), filter_var('false', FILTER_VALIDATE_BOOLEAN)]), "\n";
Output
[1,"a",[0]]
[true,false]