boolval

Converts any value to a boolean using PHP's standard truthiness rules.

Signature
boolval(mixed $value): bool
  • Added in PHP 5.5.0

Parameters

ParameterTypeDescription
$valuemixedThe value being converted to a boolean. Any type is accepted.

Return value

boolThe boolean value of $value.

boolval() gives you PHP's boolean cast as a named function. The result is identical to writing (bool) $value, so the whole point of using it is that it is a callable: you can hand it to array_map(), usort() comparators or any code that expects a function name, which you cannot do with a cast.

The conversion rules are the ones PHP uses everywhere in if statements. Only a short list of values is falsy:

  • false, null and 0
  • 0.0 and -0.0
  • the empty string '' and the string '0'
  • the empty array []

Everything else is true, including '0.0', 'false', ' ', -1, NAN and every object.

When to reach for it

Use it to normalise loosely typed input — form fields, CSV columns, JSON values — into real booleans before they reach typed properties or a database. Do not use it to interpret human strings such as 'yes', 'off' or 'false'; those are all truthy. For that job use filter_var($v, FILTER_VALIDATE_BOOL), which understands those words and can return null for anything it does not recognise.

Example

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

When to use it

  • Normalising a checkbox or hidden field from a submitted form into a real bool before assigning it to a typed property
  • Converting a column of mixed 0/1/'' flags to booleans in one pass with array_map
  • Coercing a nullable database value into a strict bool for a JSON API response

Frequently asked questions

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

There is no difference in the result; boolval() performs exactly the same conversion as the (bool) cast. Use boolval() when you need a callable, for example as the second argument to array_map().

Why does boolval('false') return true?

Because 'false' is a non-empty string, and PHP treats every non-empty string except '0' as true. Use filter_var($s, FILTER_VALIDATE_BOOL) if you need the word 'false' to mean false.

Does boolval() work on arrays and objects?

Yes. An array is false only when empty, and every object is true, including an empty stdClass. It never emits a warning for these types.

More examples

Normalising a set of form flags

The first-class callable syntax boolval(...) lets array_map use the conversion directly. Note the trap in the output: 'off' becomes true, because a non-empty string other than "0" is always truthy. Only 'notify' and 'push' come back false.

Example · php
<?php
$input = ['notify' => '0', 'sms' => '1', 'push' => '', 'email' => 'off'];

$flags = array_map(boolval(...), $input);

var_dump($flags);

boolval() versus FILTER_VALIDATE_BOOL

boolval() reports true for 'yes', 'no', 'on' and 'off' alike, because it only looks at emptiness. filter_var() with FILTER_VALIDATE_BOOL understands the words and returns false for 'no' and 'off'. Use filter_var() whenever the string came from a human.

Example · php
<?php
foreach (['yes', 'no', 'on', 'off', '1', '0'] as $s) {
    printf("%-4s boolval=%-5s filter_var=%s\n",
        $s,
        var_export(boolval($s), true),
        var_export(filter_var($s, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE), true)
    );
}

Objects and arrays are not scalar-like

An empty array is the only falsy array — [0] is true because it has one element. Every object is true even when it has no properties, and NAN is true because it is a non-zero float.

Example · php
<?php
var_dump(boolval([]));
var_dump(boolval([0]));
var_dump(boolval(new stdClass()));
var_dump(boolval(NAN));

Discussion

  • Be the first to comment on this lesson.