I wonder how PHP handles true/false comparison internally. I understand that true is defined as 1 and false is defined as 0. When I do if("a"){ echo "true";}
it echos "true". How does PHP recognize "a" as 1 ?
A boolean value represents a truth value, which is either true or false . PHP evaluates the following values to false: false, 0, 0.0, empty string (“”), “0”, NULL, an empty array; other values are true .
The is_bool() function checks whether a variable is a boolean or not. This function returns true (1) if the variable is a boolean, otherwise it returns false/nothing.
Definition and Usage This is one of the scalar data types in PHP. A boolean data can be either TRUE or FALSE. These are predefined constants in PHP. The variable becomes a boolean variable when either TRUE or FALSE is assigned.
When converting to boolean, the following values are considered FALSE: the boolean FALSE itself. the integer 0 (zero) the float 0.0 (zero)
This is covered in the PHP documentation for booleans and type comparison tables.
When converting to boolean, the following values are considered FALSE:
FALSE
itself0
(zero)0.0
(zero)'0'
NULL
(including unset variables)Every other value is considered TRUE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With