boolval

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

boolvalGet the boolean value of a variable

Description

boolval(mixed $value): bool

Returns the bool value of value.

Parameters

value

The scalar value being converted to a bool.

Return Values

The bool value of value.

Examples

Example #1 boolval() examples

<?php
echo '0: '.(boolval(0) ? 'true' : 'false')."\n";
echo
'42: '.(boolval(42) ? 'true' : 'false')."\n";
echo
'0.0: '.(boolval(0.0) ? 'true' : 'false')."\n";
echo
'4.2: '.(boolval(4.2) ? 'true' : 'false')."\n";
echo
'"": '.(boolval("") ? 'true' : 'false')."\n";
echo
'"string": '.(boolval("string") ? 'true' : 'false')."\n";
echo
'"0": '.(boolval("0") ? 'true' : 'false')."\n";
echo
'"1": '.(boolval("1") ? 'true' : 'false')."\n";
echo
'[1, 2]: '.(boolval([1, 2]) ? 'true' : 'false')."\n";
echo
'[]: '.(boolval([]) ? 'true' : 'false')."\n";
echo
'stdClass: '.(boolval(new stdClass) ? 'true' : 'false')."\n";
?>

The above example will output:

0:        false
42:       true
0.0:      false
4.2:      true
"":       false
"string": true
"0":      false
"1":      true
[1, 2]:   true
[]:       false
stdClass: true

See Also

add a note

User Contributed Notes 7 notes

up
54
Matt M
10 years ago
To anyone like me who came here looking for a way to turn any value into a 0/1 that will fit into a MySQL boolean (tinyint) field:<?php$tinyint = (int) filter_var($valToCheck, FILTER_VALIDATE_BOOLEAN);?>tinyint will be 0 (zero) for values like string "false", boolean false, int 0tinyint will be 1 for values like string "true", boolean true, int 1Useful if you are accepting data that might be from a language like Javascript that sends string "false" for a boolean false.
up
44
Anonymous
4 years ago
boolval('false') returns true.boolval('False') return true.
up
54
info at lomalkin dot ru
12 years ago
<?// Hack for old php versions to use boolval()if (!function_exists('boolval')) {        function boolval($val) {                return (bool) $val;        }}?>
up
25
raphael.satyago
10 years ago
function is_true($val, $return_null=false){    $boolval = ( is_string($val) ? filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) : (bool) $val );    return ( $boolval===null && !$return_null ? false : $boolval );}// Return Values:is_true(new stdClass);      // trueis_true([1,2]);             // trueis_true([1]);               // trueis_true([0]);               // trueis_true(42);                // trueis_true(-42);               // trueis_true('true');            // trueis_true('on')               // trueis_true('off')              // falseis_true('yes')              // trueis_true('no')               // falseis_true('ja')               // falseis_true('nein')             // falseis_true('1');               // trueis_true(NULL);              // falseis_true(0);                 // falseis_true('false');           // falseis_true('string');          // falseis_true('0.0');             // falseis_true('4.2');             // falseis_true('0');               // falseis_true('');                // falseis_true([]);                // false
up
23
Babak Bandpey
11 years ago
I believe that the double negation !! can perform the same task with the same result if your PHP is not up2datevar_dump(!!1, !!0, !!"test", !!"");outputs:boolean trueboolean falseboolean trueboolean falseMay the life be good to you.
up
8
uasenior at gmail dot com
5 years ago
<?// Some way to print boolean value as string$b = true;echo ['false', 'true'][$b];?>
up
-4
Anonymous
11 years ago
A misspell in v2.0. :-)Hack v2.1<?phpif( ! function_exists('boolval')){    /**     * Get the boolean value of a variable     *     * @param mixed The scalar value being converted to a boolean.     * @return boolean The boolean value of var.     */    function boolval($var)    {        return !! $var;    }}?>
To Top