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

前端 未结 9 738
隐瞒了意图╮
隐瞒了意图╮ 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:12

    $variables = array('name', 'number', 'address');
    
    foreach($variables as $variable_name){
    
       if(isset($_POST[$variable_name])){
          echo 'Variable: '.$variable_name.' is set
    '; }else{ echo 'Variable: '.$variable_name.' is NOT set
    '; } }

    Or, Iterate through each $_POST key/pair

    foreach($_POST as $key => $value){
    
       if(isset($value)){
          echo 'Variable: '.$key.' is set to '.$value.'
    '; }else{ echo 'Variable: '.$key.' is NOT set
    '; } }

    The last way is probably your easiest way - if any of your $_POST variables change you don't need to update an array with the new names.

提交回复
热议问题