The boolval() function is an inbuilt function in PHP which gives the Boolean value for a given expression.
boolean boolval( $expr )
Parameter: This function accepts only one parameter as shown in above syntax and described below:
- $expr: The expression or the scalar which you want to change into boolean value. It can be a string type, integer type and etc.
Return Value: This function will return a boolean value based on the below conditions.
- if $expr is evaluated to boolean true it will return TRUE.
- if $expr is evaluated to boolean false it will return FALSE.
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 – in this 0 is false and everything else is true.
- float – in this 0.0 is false and everything else is true.
- string – “0” and null string are false and everything else is true (even “0.0”)
- array – empty array is false and everything else is true
- object – here null is false and everything else is true
- null – null is always false.
Below program illustrate the boolval() function in PHP:
boolval of 3: true boolval of -3 : true boolval of 0: false boolval of 3.5: true boolval of -3.5: true boolval of 0.0: false boolval of "1": true boolval of "0": false boolval of "0.0": true boolval of "xyz": true boolval of "": false boolval of [1, 5]: true boolval of : false boolval of NULL: false
This article is attributed to GeeksforGeeks.org