Issues with my code

Hi All,

Below is my code where I am having issues displaying the number to a letter grade. I have tried everything. I am new to programming so I am not sure what I am doing wrong.

[php]<?php
function grade($score){
if ($score>=95)
{$grade = “A.”;}
else if($score>=95 && $score<90)
{$grade = “A-.”;}
else if($score>=90 && $score<85)
{$grade = “B+.”;}
else if($score>=85 && $score<80)
{$grade = “B.”;}
else if($score>=80 && $score<75)
{$grade = “B-.”;}
else if($score>=75 && $score<70)
{$grade = “C+.”;}
else if($score>=70 && $score<65)
{$grade = “C.”;}
else if($score>=65 && $score<60)
{$grade = “C-.”;}
else($score>=60 && $score<0)
{$grade = “F.”;}

$html="if you get a $score your grade is $grade
";
return $html;

}
$x =“20”;
echo grade($score);

$x =“98”;
echo grade($x);
?>[/php]

The short answer is, follow your logic.

[php]else if($score>=95 && $score<90)[/php]

if $score is greater than or equal to 95 and $score is less than 90.

Also else($score>=60 && $score<0)
Are you intending it to be else if($score>=60 && $score<0)

Here is your code working correctly.
I have added comments so you can see the issues you were having.

[php]

<?php function grade($score){ // The first statement is correct. // If the score is greater than or equal to 95 it's an A. if ($score >= 95) { $grade = "A."; // This is where it started to go wrong // else if($score>=95 && $score<90) // The above statement is saying if the score is above or equal to 95 and less than 90 its an A- // That statement can never be true. // I have changed it so if the score is less than 95 ( Not an A. ) and greater or equal to 90 (Not a B+.) it's an A- } elseif ($score < 95 && $score >= 90) { $grade = "A-."; } elseif ($score < 90 && $score >= 85) { $grade = "B+."; } elseif ($score < 85 && $score >= 80) { $grade = "B."; } elseif ($score < 80 && $score >= 75) { $grade = "B-."; } elseif ($score < 75 && $score >= 70) { $grade = "C+."; } elseif ($score < 70 && $score >= 65) { $grade = "C."; } elseif ($score < 65 && $score >= 60) { $grade = "C-."; // When adding the final else it can't take any logic. // Since all the other scores are being figured out before this point we know it is going to be an F if it doesn't match any. } else { $grade = "F."; } return "if you get a $score your grade is $grade
"; } $x = 98; echo grade($x); [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service