I am using codeigniter form validation. To display form errors it uses this
$this->data['message'] = (validation_errors() ?
validation_errors() :
($this->auth_lib->errors() ?
$this->auth_lib->errors() :
$this->session->flashdata('message')))
I don't understand this syntax. I think it is an if else statement. It is very hard to understand.
Can anyone please convert this to a normal if else statement?
Because now i am going to change the error message format:
$this->message->set_error($msg=array('Test 1','Test 2'));
$message=$this->message->get_message();
$this->data['message']=$message;
Anyone, please simplify the syntax. Thanks.
a ? b : c == if (a) {b} else {c}
if (validation_errors())
{
$this->data['message'] = validation_errors();
}
else if ($this->auth_lib->errors())
{
$this->data['message'] = $this->auth_lib->errors();
}
else
{
$this->data['message'] = $this->session->flashdata('message');
}
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->auth_lib->errors() ? $this->auth_lib->errors() : $this->session->flashdata('message')))
is a multiple ternary operation equivalent to:
if ( validation_errors() )
$this->data['message'] = validation_errors();
elseif ( $this->auth_lib->errors() )
$this->data['message'] = $this->auth_lib->errors();
else
$this->data['message'] = $this->session->flashdata('message');
The code you posted is using ternary operators. They can be very handy but also confusing at times if nesting several of them. Here's the equivalent written without the ternary operators...
if(validation_errors())
{
$this->data['message'] = validation_errors();
}
else
{
if($this->auth_lib->errors())
{
$this->data['message'] = $this->auth_lib->errors();
}
else
{
$this->data['message'] = $this->session->flashdata('message');
}
}