Better way to write if/else conditions I've got the following code where:


$urlParams is the school year from 7 to 11
$Comms['Overall'] is the total amount of "commendations" a student has


This code iterates through the numbers based on the school year to determine which "award" a student has got.

Is there a better way to write this code?

if($urlParams['y'] == 7){
    if((300 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 599)){ $Award = "Bronze"; }
    elseif((600 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 899)){ $Award = "Silver"; }
    elseif((900 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 1199)){ $Award = "Gold"; }
    elseif((1200 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 1499)){ $Award = "Platinum"; }
    elseif($Comms['Overall'] &gt;= 1500){$Award = "Diamond";}
}

elseif($urlParams['y'] == 8){
    if((250 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 549)){ $Award = "Bronze"; }
    elseif((550 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 849)){ $Award = "Silver"; }
    elseif((850 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 1099)){ $Award = "Gold"; }
    elseif((1100 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 1299)){ $Award = "Platinum"; }
    elseif($Comms['Overall'] &gt;= 1300){ $Award = "Diamond"; }  
}

elseif($urlParams['y'] == 9){
    if((200 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 449)){ $Award = "Bronze"; }
    elseif((450 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 699)){ $Award = "Silver"; }
    elseif((700 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 899)){ $Award = "Gold"; }
    elseif((900 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 1099)){ $Award = "Platinum"; }
    elseif($Comms['Overall'] &gt;= 1100){ $Award = "Diamond"; }
}

elseif($urlParams['y'] == 10){
    if((150 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 399)){ $Award = "Bronze"; }
    elseif((400 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 599)){ $Award = "Silver"; }
    elseif((600 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 799)){ $Award = "Gold"; }
    elseif((800 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 999)){ $Award = "Platinum"; }
    elseif($Comms['Overall'] &gt;= 1000){ $Award = "Diamond"; } 
}

elseif($urlParams['y'] == 11){
    if((150 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 299)){ $Award = "Bronze"; }
    elseif((300 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 499)){ $Award = "Silver"; }
    elseif((500 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 699)){ $Award = "Gold"; }
    elseif((700 &lt;= $Comms['Overall']) &amp;&amp; ($Comms['Overall'] &lt;= 849)){ $Award = "Platinum"; }
    elseif($Comms['Overall'] &gt;= 850){ $Award = "Diamond"; }
 }
You should separate these magic numbers into a new data structure. Then have some code run over it. This will be much nicer to read.

For example:

function getAward($year, $commends) {
    $commendationsNeeded = [
        7 =&gt; [
            "Bronze" =&gt; 300,
            "Silver" =&gt; 600,
            "Gold" =&gt; 900,
            "Platinum" =&gt; 1200,
            "Diamond" =&gt; 1500
        ],
        8 =&gt; [
            "Bronze" =&gt; 250,
            "Silver" =&gt; 550,
            "Gold" =&gt; 850,
            "Platinum" =&gt; 1100,
            "Diamond" =&gt; 1300
        ]
    ];
    $maxScore = 0;
    $maxAward = "None";
    foreach ($commendationsNeeded[$year] as $award =&gt; $needed) {
        if ($needed &lt;= $commends &amp;&amp; $maxScore &lt; $needed) {
            $maxScore = $needed;
            $maxAward = $award;
        }
    }
    return $maxAward;
}

echo getAward(7, 700); // Prints "Silver"