Search code examples
phpformspostisset

How to detect when "0" is supplied as a form/$_POST value?


I'm trying to determine whether or not there is a value passed, but the value CAN be 0 ...

isset always returns true, and empty returns false because the value is 0

How can I get around this?


Solution

  • try

    bool array_key_exists ( mixed $key , array $array )
    

    like

    if (array_key_exists("var1", $_POST)) {
        // positive case, var1 was posted
        if ($_POST["var1"] == 0){
            // var1 was posted and 0
        }else{
            // var1 was posted and is not 0
        }
    }
    

    more details are given at the docs.