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

前端 未结 9 887
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 06:02

    The following is a custom function that take an array for the required posted elements as a parameter and return true if they all posted and there is no any of them is empty string '' or false if there is at least one of them is not:

    function checkPosts($posts){
      if (!is_array($posts)) return "Error: Invalid argument, it should be an array";
      foreach ($posts as $post){
        if (!isset($_POST[$post]) || $_POST[$post] == '') return false;
      }
      return true;
    } 
    // The structure of the argument array may be something like:
    
    $myPosts = array('username', 'password', 'address', 'salary');
    

提交回复
热议问题