如何检查多个isset($ _ POST ['something'])?

时间:2015-11-14 13:45:42

标签: php codeigniter-2

如何检查多个isset($_POST['something'])?用下面提到的代码函数调用没有设置什么?我做错了什么

我的代码在这里

if(!isset($_POST['username']) || $_POST['email'] || $_POST['password'] || $_POST['confirm_pass'] || $_POST['gender'] || $_POST['country']== "") 

{   $username = $this->input->post('username');
    $email = $this->input->post('email');    
    $password = $this->input->post('password');
    $confirm_password = $this->input->post('confirm_pass');
    $gender = $this->input->post('gender');
    $country = $this->input->post('country');
    $this->signupdata->submit_data($username,$email,$password,$confirm_password,$gender,$country);     
 exit(); }
 $this->load->view('signup_view'); 

3 个答案:

答案 0 :(得分:2)

isset接受多个变量

在您的情况下,您可以

if (!isset($_POST['username'], $_POST['email'], $_POST['password'], $_POST['confirm_pass'], $_POST['gender'], $_POST['country']) ) {

}

我还应该提到isset只有在设置了所有变量

时才会返回true
  

http://php.net/manual/en/function.isset.php

     

来自Doc:

     

如果提供了多个参数,则isset()将仅返回TRUE   如果设置了所有参数。评估从左到右   并在遇到未设置的变量时立即停止。

答案 1 :(得分:0)

检查空/未设置字段的更好方法可能是这样的。玩这个以达到预期的效果:

<?php   
    $req = array("username", "email", "password", "confirm_pass", "gender", "country");
    foreach($req AS $r) {
        if(empty($_POST[$r])) {
            $error = $r . " is a required field";
        } else {
            $username = $this->input->post('username');
            $email = $this->input->post('email');    
            $password = $this->input->post('password');
            $confirm_password = $this->input->post('confirm_pass');
            $gender = $this->input->post('gender');
            $country = $this->input->post('country');
            $this->signupdata->submit_data($username,$email,$password,$confirm_password,$gender,$country);     
            exit();
            $this->load->view('signup_view');
        }    
    }
?>

答案 2 :(得分:0)

如果未设置字符串,

empty将返回错误或通知。

我个人使用!isset功能。