jsuissa
jsuissa

Reputation: 1762

Evaluating multiple conditions at once using PHP

I want to change this code sample so that all these conditions are evaluated at the same time. If more than one condition is true multiple arguments can be passed to the 'signup' function.

I originally tried using switch, but each condition needs to be able to evaluate a different condition.

Any help pointing me in the right direction on this would be great.

    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.");

    }

Upvotes: 0

Views: 247

Answers (2)

Zombaya
Zombaya

Reputation: 2258

You could put all errors/faulty conditions in an array and pass that to $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);
}

Upvotes: 2

Ben
Ben

Reputation: 62444

Use the && operator

a = 1; b = 2; if (a == 1 && b == 2) { echo "this would show up on the page"; }

Upvotes: 0

Related Questions