boolval()
Our article will focus on the topic of the PHP boolval() function and its use in web development. We will cover it comprehensively to provide readers with all
Introduction
The boolval() function in PHP is a type-casting function that converts any value to its boolean equivalent — either true or false. Introduced in PHP 5.5, it follows the same rules PHP uses everywhere a value is interpreted as a condition (such as inside an if statement), so it is a clear, explicit way to ask "is this value truthy?".
boolval() does exactly the same thing as the (bool) cast. The reason it exists is that, unlike a cast, a function can be passed as a callback — for example to array_map() — which a language construct like (bool) cannot.
This page covers the syntax, what each type converts to, how boolval() compares to the (bool) cast, and the common pitfalls (such as the string "0").
Basic Syntax
The basic syntax of the boolval() function is as follows:
Basic Syntax of PHP boolval() function
boolval(mixed $value): boolThe $value parameter is the value to be converted. The function always returns a boolean: true for any truthy value and false for any falsy value. It has no side effects and never throws.
Which values are falsy?
PHP treats only a fixed set of values as false. Everything else is true. Memorizing this short list is the key to using boolval() correctly:
falseitself- the integer
0and the float0.0(and-0.0) - the empty string
""and the string"0" - the empty array
[] null- an unset / undefined variable
The string "0" is the classic gotcha: it is non-empty, yet PHP considers it falsy. Note that "0.0", " " (a space), and "false" are all truthy strings.
Example Usage
Here is an example of how the boolval() function can be used in PHP:
Example Usage of PHP boolval()
Here boolval() converts $var1 (a non-zero integer) to true and $var2 (zero) to false. We wrap the result in var_export() so the boolean prints as the literal text true / false — a plain echo would print 1 for true and nothing at all for false, which makes results hard to read.
boolval() vs the (bool) cast
For converting a single value, boolval($x) and (bool) $x are interchangeable and produce identical results:
<?php
$value = "0";
var_export(boolval($value)); // false
echo "\n";
var_export((bool) $value); // falseSo when should you reach for boolval() instead of (bool)? Whenever you need to pass the conversion as a callable. A cast is a language construct, not a function, so it cannot be handed to higher-order functions like array_map() or array_filter(). boolval can:
Filtering an array down to its truthy values
Each element is converted to a boolean. 1, "hello", and [1] become true; the rest — including the deceptive "0" — become false.
Common pitfall: the string "0"
Because boolval() mirrors PHP's truthiness rules, the string "0" evaluates to false. This trips up code that validates user input, since form fields always arrive as strings:
<?php
$input = "0"; // e.g. $_POST['quantity']
if (boolval($input)) {
echo "treated as true\n";
} else {
echo "treated as false\n"; // this branch runs
}If you actually want "is this string non-empty?" rather than "is this truthy?", compare against "" explicitly with a strict check ($input !== "") instead of relying on boolval().
Best Practices for Using boolval()
To ensure efficient use of the boolval() function in PHP, it is important to follow some best practices:
Use strict comparisons
When using the boolval() function, it is important to use strict comparisons (===) instead of loose comparisons (==). This is because loose comparisons can result in unexpected behavior.
Use appropriate variable types
When using the boolval() function, it is important to ensure that the variable being converted is of the appropriate type. For more details on how different data types convert to booleans, refer to the PHP manual on type juggling.
Type conversion reference
Understanding how different PHP types convert to booleans helps avoid unexpected results:
| Type | Converts to true when | Converts to false when |
|---|---|---|
string | Non-empty string | Empty string "" |
int / float | Non-zero value | 0 or 0.0 |
array | Non-empty array | Empty array [] |
null | Never | Always |
Related functions
boolval() is one of PHP's family of type-conversion helpers. Reach for the one that matches the type you need:
intval()— convert a value to an integer.floatval()— convert a value to a float.strval()— convert a value to a string.settype()— change a variable's type in place.gettype()/is_bool()— inspect a variable's current type.
For a broader overview of how values move between types, see PHP data types.
Conclusion
boolval() converts any value to a boolean using PHP's standard truthiness rules, returning false only for the short, fixed list of falsy values (false, 0, 0.0, "", "0", [], null, and unset variables) and true for everything else. It behaves exactly like the (bool) cast, with the added benefit of being usable as a callback in functions like array_map(). Keep the "0" pitfall in mind when validating string input, and prefer strict comparisons when you need precise control.