jeudi 9 février 2017

How to optimize numerous 'else if'?

There are data from the user that I need to check for membership in a particular range, and depending on this output a different result. I use this method:

if($var < 1000) {
    do one
}
else if ($var < 2000) {
    do two
}
else if ($var < 3000) {
    do three
}
....

But it looks unprofessional. How can I optimize this code? /without 'switch' - it's almost the same/

Aucun commentaire:

Enregistrer un commentaire