PHP boolval() Function
The PHP boolval() function returns the boolean value of a variable. Below is the list of different variable types along with their values which will evaluate to TRUE or FALSE when converted to boolean value:
- integer: 0 is false and everything is true.
- float: 0.0 is false and everything else is true.
- string: "0", "0.0" and empty string are false and everything else is true.
- array: empty array is false and everything else is true.
- object: null object is false and everything else is true.
- null: null is always false.
Syntax
boolval(variable)
Parameters
variable |
Required. Specify the expression or scalar value being converted to a bool. |
Return Value
Returns bool value of the variable.
Example:
The example below shows the usage of boolval() function.
<?php echo '0: '.(boolval(0) ? 'true' : 'false')."\n"; echo '10: '.(boolval(10) ? 'true' : 'false')."\n"; echo '0.0: '.(boolval(0.0) ? 'true' : 'false')."\n"; echo '7.2: '.(boolval(7.2) ? 'true' : 'false')."\n"; echo '"": '.(boolval("") ? 'true' : 'false')."\n"; echo '"xyz": '.(boolval("xyz") ? 'true' : 'false')."\n"; echo 'NULL: '.(boolval(NULL) ? 'true' : 'false')."\n"; echo '"0": '.(boolval("0") ? 'true' : 'false')."\n"; echo '"1": '.(boolval("1") ? 'true' : 'false')."\n"; echo 'TRUE: '.(boolval(TRUE) ? 'true' : 'false')."\n"; echo 'FALSE: '.(boolval(FALSE) ? 'true' : 'false')."\n"; echo '(10>5): '.(boolval((10>5)) ? 'true' : 'false')."\n"; echo '[1, 2]: '.(boolval([1, 2]) ? 'true' : 'false')."\n"; echo '[]: '.(boolval([]) ? 'true' : 'false')."\n"; echo 'array(): '.(boolval(array()) ? 'true' : 'false')."\n"; echo 'array(1, 2): '.(boolval(array(1, 2)) ? 'true' : 'false')."\n"; echo 'stdClass: '.(boolval(new stdClass) ? 'true' : 'false')."\n"; ?>
The output of the above code will be:
0: false 10: true 0.0: false 7.2: true "": false "xyz": true NULL: false "0": false "1": true TRUE: true FALSE: false (10>5): true [1, 2]: true []: false array(): false array(1, 2): true stdClass: true
❮ PHP Variable Handling Reference