how to check multiple $_POST variable for existence using isset()?

前端 未结 9 873
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 05:17

I need to check if $_POST variables exist using single statement isset.

if (isset$_POST[\'name\']  &&  isset$_POST[\'number\']  &&am         


        
9条回答
  •  猫巷女王i
    2020-12-18 06:20

    That you are asking is exactly what is in isset page

    isset($_POST['name']) && isset($_POST['number']) && isset($_POST['address'])
    

    is the same as:

    isset($_POST['name'], $_POST['number'], $_POST['address'])
    

    If you are asking for a better or practical way to assert this considering that you already have all the required keys then you can use something like:

    $requiredKeys = ['name', 'number', 'address'];
    $notInPost = array_filter($requiredKeys, function ($key) {
        return ! isset($_POST[$key]);
    });
    

    Remember, isset does not return the same result as array_key_exists

提交回复
热议问题