Calculate Letter Grade From Percentage

Description: A simple function that translates a score (based on 100) to the equivalent letter grade. You can easily adjust the ranges to alter the scale and add additional letter grades (like A- or B+) to correspond to those scores.
Tested Platform: PHP 5+
Language: PHP
// Score is an integer or float that is based on 0 - 100 scale.
function letterGrade($score) {
    if ($score >= 90) { return "A"; }
    else if ($score >= 80) { return "B"; }
    else if ($score >= 70) { return "C"; }		
    else if ($score >= 60) { return "D"; }
    else { return "F"; }
}

Posted: March 18, 2023

Return to the snippets listing