<?
// Hack for old php versions to use boolval()
if (!function_exists('boolval')) {
function boolval($val) {
return (bool) $val;
}
}
?>
boolval
(PHP 5 >= 5.5.0)
boolval — Get the boolean value of a variable
Elenco dei parametri
-
var -
The scalar value being converted to a boolean.
Valori restituiti
The boolean value of var.
Esempi
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([1, 2]) ? 'true' : 'false')."\n";
echo '[]: '.(boolval([]) ? 'true' : 'false')."\n";
echo 'stdClass: '.(boolval(new stdClass) ? 'true' : 'false')."\n";
?>
Il precedente esempio visualizzerĂ :
0: false 42: true 0.0: false 4.2: true "": false "string": true [1, 2]: true []: false stdClass: true
Vedere anche:
- floatval() - Restituisce il valore di una variabile di tipo float
- intval() - Estrae il valore intero da una variabile
- strval() - Restituisce il valore di una variabile interpretato come stringa
- settype() - Definisce il tipo di una variabile
- is_bool() - Verifica se una variabile è di tipo boolean
- Type juggling
info at lomalkin dot ru ¶
3 months ago
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);
