boolval
(PHP 5 >= 5.5.0)
boolval — Get the boolean value of a variable
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 '"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
- floatval() - Get float value of a variable
- intval() - Get the integer value of a variable
- strval() - Get string value of a variable
- settype() - Set the type of a variable
- is_bool() - Finds out whether a variable is a boolean
- Type juggling
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Функции для работы с переменными
- boolval
- debug_zval_dump
- doubleval
- empty
- floatval
- get_defined_vars
- get_resource_type
- gettype
- import_request_variables
- intval
- is_array
- is_bool
- is_callable
- is_double
- is_float
- is_int
- is_integer
- is_long
- is_null
- is_numeric
- is_object
- is_real
- is_resource
- is_scalar
- is_string
- isset
- print_r
- serialize
- settype
- strval
- unserialize
- unset
- var_dump
- var_export
Коментарии
<?
// Hack for old php versions to use boolval()
if (!function_exists('boolval')) {
function boolval($val) {
return (bool) $val;
}
}
?>
A misspell in v2.0.
:-)
Hack v2.1
<?php
if( ! 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;
}
}
?>
I believe that the double negation !! can perform the same task with the same result if your PHP is not up2date
var_dump(!!1, !!0, !!"test", !!"");
outputs:
boolean true
boolean false
boolean true
boolean false
May the life be good to you.
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 0
tinyint will be 1 for values like string "true", boolean true, int 1
Useful if you are accepting data that might be from a language like Javascript that sends string "false" for a boolean false.
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); // true
is_true([1,2]); // true
is_true([1]); // true
is_true([0]); // true
is_true(42); // true
is_true(-42); // true
is_true('true'); // true
is_true('on') // true
is_true('off') // false
is_true('yes') // true
is_true('no') // false
is_true('ja') // false
is_true('nein') // false
is_true('1'); // true
is_true(NULL); // false
is_true(0); // false
is_true('false'); // false
is_true('string'); // false
is_true('0.0'); // false
is_true('4.2'); // false
is_true('0'); // false
is_true(''); // false
is_true([]); // false
<?
// Some way to print boolean value as string
$b = true;
echo ['false', 'true'][$b];
?>
boolval('false') returns true.
boolval('False') return true.