Signature
boolval(mixed $value): boolPurpose
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
0and the floats0.0and-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.
$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";
}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.
$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";{"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.
$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";[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.NANis true even though it is not a usable number, and PHP 8.5 warns about the conversion. Test withis_nan()first.-0.0is false, the same as0.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
- Treating
"false"as false. Any non-empty string other than"0"is true. Parse user or configuration text deliberately, withfilter_var()or an explicit comparison. - Using
boolval()to validate input. It only reports truthiness. It cannot tell you whether"abc"is a legitimate boolean setting. - Expecting
array_filter()to drop onlynull. Withboolvalas the callback (or with no callback),0,"0",""and[]are removed too. This can silently discard valid zero values. - Assuming it mutates the variable.
boolval()returns a new value and leaves its argument unchanged. To change the type of a variable in place, usesettype(). - Confusing it with strict checks.
boolval($x)being true does not mean$x === true. Useis_bool()or===when the exact type matters.
Related functions
intval(),floatval()andstrval()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()withFILTER_VALIDATE_BOOLEANinterprets 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
$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";
}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
$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";{"empty array":false,"array with null":true,"null":false,"object":true,"negative zero":false}
$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";[1,"a",[0]] [true,false]