samedi 23 avril 2016

Avoid multiple if statements in PHP (use array or some other option)

I am creating a conversion table using PHP and I have to check the user's input against A LOT of scenarios and I started by using if statements but this doesn't seem to be efficient whatsoever and I was hoping for an easier way to go through all the scenarios.

I looked into ternary and switch options but those don't seem to do what I need it to do and I also considered array's (the option I think I need to use)

What I'm trying to do: The user enters in a grade level and scores for a category. Based on the sum of those scores and the grade level, I need to compare them to get two other scores Example code:

if ($grade == 1  && $sumScore <= 5)
 {
   $textscore = 'Beginning';
 }

etc....

There are 13 grades and 4 categories I need to go through all with their own "raw scores" to consider to get these other scores. How can I avoid using a ton of If/Else if statements?

Thanks!!

Aucun commentaire:

Enregistrer un commentaire