mercredi 6 janvier 2016

multiple php if statements in one ternary operator

I have multiple if statements for checking value of four Checkboxes. but i see my code not good and contains duplicate code in every if statement.

I know use ternary operator for one if statement condition. But i want put below my code in one ternary operator. How can i do this?

INFO: ternary operator standard use condition ? action_if_true: action_if_false;

if (!empty($item->divideto_2)) {
    $col = 6;
    $divide_to = 12 / $col;
}
if (!empty($item->divideto_3)) {
    $col = 4;
    $divide_to = 12 / $col;
}
if (!empty($item->divideto_4)) {
    $col = 3;
    $divide_to = 12 / $col;
}
if (!empty($item->divideto_6)) {
    $col = 2;
    $divide_to = 12 / $col;
}

Aucun commentaire:

Enregistrer un commentaire