else conditional like this
if ($cond1) {
return $val1;
} else if ($cond2) {
return $val2;
} else if ($cond3) {
return $val3;
} else {
return $val4;
}
can be written as
return $cond1 ? $val1 : ($cond2 ? $val2 : ($cond3 ? $val3 : $val4));
This is the beautiful side of ternary, complicated things can be done in one line.
Обсуждают сегодня