Test if string could be boolean PHP

后端 未结 8 973
爱一瞬间的悲伤 2021-02-20 17:51

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:

  • 2021-02-20 18:21

    No you got it, there isn't anything more you can do, you got all possible values that would normally be considered as true or false and you're doing the comparison the right way, you COULD optimize it using an IN_ARRAY maybe, but even so, i find this version quite good already.

    0 讨论(0)
  • 2021-02-20 18:21

    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?

    0 讨论(0)
  • 2021-02-20 18:24

    There's, by the way, a cleaner way of writing it:

    function checkBool($string){
        $string = strtolower($string);
        return (in_array($string, array("true", "false", "1", "0", "yes", "no"), true));

    But yes. The one you wrote down is the only way.

    0 讨论(0)
  • 2021-02-20 18:25

    You can either use is_bool() or as suggested on php.net:

    $myString = "On";
    $b = filter_var($myString, FILTER_VALIDATE_BOOLEAN);


    The latter one will accept strings like "on" and "yes" as true as well.

    0 讨论(0)
  • 2021-02-20 18:28

    Your checkBool() is quite right, IMHO, though there's a problem with the resulting SQL code. You can use TRUE and FALSE, but you must be aware that they aren't strings:

    The constants TRUE and FALSE evaluate to 1 and 0, respectively. The constant names can be written in any lettercase.

    So where it says this:

    "SELECT * FROM my_table WHERE male='".$_GET['male']."'"

    ... it should say this:

    'SELECT * FROM my_table WHERE male='.$_GET['male']

    It'd feel better if checkBool() was actually convertToBool() and you would feed your query with its result value rather than the original $_GET, but your code is not really wrong.

    BTW, I'm assuming that you are using a BOOL column type. This is what the manual says:

    These types are synonyms for TINYINT(1). A value of zero is considered false. Nonzero values are considered true

    Of course, it's up to you whether to use BOOL, ENUM, CHAR(1) or anything else, as well as whether to accept 33 as synonym for TRUE ;-)

    0 讨论(0)
  • 2021-02-20 18:29

    If you use the flag FILTER_NULL_ON_FAILURE, filter_var() will work nicely:

    function CheckBool($Value) {
      return null !== filter_var($Value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
    0 讨论(0)