dismiss Step into the future! Click here to switch to the beta php.net site
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

debug_zval_dump> <Variable handling Functions
[edit] Last updated: Fri, 28 Jun 2013

view this page in

boolval

(PHP 5 >= 5.5.0)

boolvalGet the boolean value of a variable

Description

boolean boolval ( mixed $var )

Returns the boolean value of var.

Parameters

var

The scalar value being converted to a boolean.

Return Values

The boolean value of var.

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 
'[1, 2]:   '.(boolval([12]) ? '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
[1, 2]:   true
[]:       false
stdClass: true

See Also



debug_zval_dump> <Variable handling Functions
[edit] Last updated: Fri, 28 Jun 2013
 
add a note add a note User Contributed Notes boolval - [3 notes]
up
0
luigi_cangiano at hotmail dot it
16 hours ago
// Hack for old php versions to use boolval()
if(!function_exists('boolval')) {
    function    boolval($var = null) {
        $back = false;

        switch(gettype($var)) {
            case 'boolean':
                $back = $var;
                break;
            case 'float':
            case 'double':
                $var = intval($var);
            case 'integer':
                $back = $var !== 0;
                break;
            case 'string':
                switch(strtolower($var)) {
                    case 'y':
                    case 's':
                    case 'true':
                    case 'vero':
                    case '1':
                        $back = true;
                        break;

                    case 'n':
                    case 'false':
                    case 'falso':
                    case '0':
                        $back = false;
                        break;
                    default:
                        $back = (bool)$var;
                }
                break;
            default:
                $back = (bool)$var;
                break;
        }

        return $back;
    }
}
up
9
info at lomalkin dot ru
3 months ago
<?

// Hack for old php versions to use boolval()

if (!function_exists('boolval')) {
        function boolval($val) {
                return (bool) $val;
        }
}
?>
up
-13
Linuxatico
1 month ago
Well, considering that is common to code in OO, this snippet is not right, this solved for me:
class A {
    public function boolVal($val)
    {
        if (!function_exists('boolval')) {
            return (bool) $val;
        }
        else { return boolval($val); }
    }
}
and in my project I do:
$a = new A();
$a->boolVal($val);

 
show source | credits | stats | sitemap | contact | advertising | mirror sites