Solution:1
The reason is that all strings evaluate to true
when converting them to boolean, except "0"
and ""
(empty string).
The following function will do exactly what you want: it behaves exactly like PHP, but will also evaluates the string "false"
as false
:
function isBoolean($value) {
if ($value && strtolower($value) !== "false") {
return true;
} else {
return false;
}
}
The documentation explains that: http://php.net/manual/en/language.types.boolean.php :
When converting to boolean, the following values are considered FALSE:
- the boolean FALSE itself
- the integer 0 (zero)
- the float 0.0 (zero)
- the empty string, and the string “0”
- an array with zero elements
- the special type NULL (including unset variables)
- SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource).