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> <Funciones de manejo de variables
[edit] Last updated: Fri, 17 May 2013

view this page in

boolval

(PHP 5 >= 5.5.0)

boolvalObtener el valor booleano de una variable

Descripción

boolean boolval ( mixed $var )

Devuelve el valor boolean de var.

Parámetros

var

El valor escalar a convertir a un boolean.

Valores devueltos

El valor boolean de var.

Ejemplos

Ejemplo #1 Ejemplos de boolval()

<?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";
?>

El resultado del ejemplo sería:

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

Ver también



add a note add a note User Contributed Notes boolval - [2 notes]
up
0
Linuxatico
8 days 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);
up
-1
info at lomalkin dot ru
2 months ago
<?

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

if (!function_exists('boolval')) {
        function boolval($val) {
                return (bool) $val;
        }
}
?>

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