Test if string could be boolean PHP

后端 未结 8 1913
爱一瞬间的悲伤
爱一瞬间的悲伤 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:



        
8条回答
  •  醉话见心
    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.

提交回复
热议问题