PHP boolval() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, boolval() converts any value to a boolean. It returns true for values PHP treats as truthy and false for values PHP treats as falsy.

It does the same job as the (bool) cast, but as a function you can pass to array_map() or array_filter(). The falsy values are false, 0, 0.0, an empty string, the string "0", an empty array and null. Everything else is true.

Syntax

Call boolval() with the values you need. Start with this line.

PHP
$flag = boolval($value);
CallWhat it does
boolval(1)Returns true, because any non-zero number is truthy.
boolval("")Returns false, because an empty string is falsy.
boolval("0")Returns false, because the string "0" is one of the falsy values.

boolval() never fails and never changes the original variable. It only returns a new boolean. Beware that the string "false" is true, because it is a non-empty string.

Library declarations
PHP
boolval(mixed $value): bool

Parameters

ParameterDescription
valueRequired. The value you want to convert to a boolean. It can be any type.

Return Value

Return ValueDescription
boolfalse when the value is falsy (false, 0, 0.0, "", "0", [] or null). true for every other value.

PHP Examples

Example 1 — Converting Common Values

Pass a few everyday values to boolval() and print the result with var_dump().

PHP
<?php
var_dump(boolval(1));
var_dump(boolval(0));
var_dump(boolval("hello"));
var_dump(boolval(""));

How It Works

  1. boolval(1) is true, because any number other than zero is truthy.
  2. boolval(0) is false, because zero is falsy.
  3. A non-empty string such as "hello" is true.
  4. An empty string is false. var_dump() prints bool(true) or bool(false) so you can see the exact type.

Example 2 — Surprising Truthy and Falsy Values

Some values look empty but are truthy, and some look full but are falsy. This loop shows the tricky ones.

PHP
<?php
$tests = [
    'string "0"'   => "0",
    'string "0.0"' => "0.0",
    'string " "'   => " ",
    'empty array'  => [],
    'array [0]'    => [0],
    'float 0.0'    => 0.0,
];

foreach ($tests as $label => $value) {
    echo $label, ": ", boolval($value) ? "true" : "false", PHP_EOL;
}

How It Works

  1. The array pairs a readable label with each value to test.
  2. The string "0" is false, but the string "0. 0" is true, because only the exact text "0" is falsy.
  3. A string with one space is true, because it is not empty.
  4. An empty array is false, but an array holding a value (even 0) is true. Only the array size matters.

Example 3 — Reading Form-Style Values

Form fields often arrive as strings. See how boolval() treats typical checkbox and number inputs.

PHP
<?php
$input = [
    'newsletter' => 'on',
    'terms'      => '',
    'age'        => '0',
    'note'       => 'false',
];

foreach ($input as $field => $value) {
    echo $field, " = ", var_export(boolval($value), true), PHP_EOL;
}

How It Works

  1. Each field holds a string, just like data from an HTML form.
  2. The text "on" is a non-empty string, so it becomes true.
  3. An empty string and the string "0" both become false.
  4. The word "false" is still a non-empty string, so boolval() returns true. Use filter_var() when you need to read words like "false".

Frequently Asked Questions

boolval() converts any value to a boolean. It returns true for truthy values and false for falsy values such as 0, an empty string, "0", an empty array and null.
They give the same result. (bool) is a cast written inline, while boolval() is a function. The function form is handy when you need a callback, for example array_filter($list, 'boolval') to remove falsy items.
PHP only checks whether the string is empty or exactly "0". The text "false" is a normal non-empty string, so it is truthy. Use filter_var($value, FILTER_VALIDATE_BOOLEAN) to read words like "false", "no" or "off".
false, the integers 0 and -0, the floats 0.0 and -0.0, the empty string, the string "0", an empty array and null. Every other value is true, including negative numbers and objects.
Yes. Writing !!$value gives the same boolean as boolval($value). Many developers prefer boolval() or (bool) because the intent is easier to read.

Next: debug_zval_dump()

debug_zval_dump() prints detailed internal information about one or more values, including.

debug_zval_dump() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful