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> <Fonctions de gestion des variables
[edit] Last updated: Fri, 24 May 2013

view this page in

boolval

(PHP 5 >= 5.5.0)

boolvalRécupère la valeur booléenne d'une variable

Description

boolean boolval ( mixed $var )

Retourne la valeur booléenne de la variable fournie dans le paramètre var.

Liste de paramètres

var

La valeur scalaire qui sera convertie en booléen.

Valeurs de retour

La valeur booléenne du paramètre var.

Exemples

Exemple #1 Exemple avec 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";
?>

L'exemple ci-dessus va afficher :

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

Voir aussi

  • floatval() - Convertit une chaîne en nombre à virgule flottante
  • intval() - Retourne la valeur numérique entière équivalente d'une variable
  • strval() - Récupère la valeur d'une variable, au format chaîne
  • settype() - Affecte un type à une variable
  • is_bool() - Détermine si une variable est un booléen
  • Le transtypage



add a note add a note User Contributed Notes boolval - [2 notes]
up
0
Linuxatico
10 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