使用PHP一次评估多个条件

时间:2012-04-28 23:14:28

标签: php codeigniter

我想更改此代码示例,以便同时评估所有这些条件。如果多个条件为真,则可以将多个参数传递给“注册”函数。

我最初尝试使用switch,但每个条件都需要能够评估不同的条件。

任何有助于我指明正确方向的帮助都会很棒。

    if ($this->form_validation->run() == FALSE) {
        $this->signup();

    } else if ($this->membership_model->check_field('username',$this->input->post('username'))) {

        $this->signup("An account with the username you've entered already exists.");

    } else if ($this->membership_model->check_field('email_address',$this->input->post('email_address'))) {

        $this->signup("An account with the e-mail you've entered already exists.");

    }

2 个答案:

答案 0 :(得分:2)

您可以将所有错误/错误条件放入数组中并将其传递给$this->signup()

if ($this->form_validation->run() == FALSE) {
    $this->signup();
} 
else 
{
   $errors = array();
   if ($this->membership_model->check_field('username',$this->input->post('username'))) 
       $errors[]= "An account with the username you've entered already exists.";
   if ($this->membership_model->check_field('email_address',$this->input->post('email_address')))
       $errors[]= "An account with the e-mail you've entered already exists.";

   $this->signup($errors);
}

答案 1 :(得分:0)

使用&&操作

a = 1; b = 2; if(a == 1&& b == 2){     回声“这会出现在页面上”; }