I\'m getting a string from a $_GET and I want to test if it could be a boolean, before I use it for a part of a mysql query. Is there a better way of doing it than:
Boolean variables contain true or false values. To test if string could be boolean, different approaches can be used:
is_bool($var): The function is_bool() checks whether a variable’s type is Boolean and returns true or false.
Comparison using “===” or ($var===true || $var===false): If you compare the variable with the operator “===” with true and false and if it is identical to one of the values, then it is a boolean value. Since the functionality of is_bool($var) is same, the function should be preferred.
Comparison using “==” or ($var == true || $var == false): If you use “==” as an operator instead, the test is more tolerant. For example, 0(integer) or “”(empty string) would also result in a Boolean value.
Read more in How to check if a variable is a Boolean in PHP?