6

This is just a simple check to see what letter grade to output. Is there any faster and more efficient way to achieve the objective?

if ( $grade >= 90 ) {
        echo "A";
    } elseif ( $grade >= 80 ) {
        echo "B";
    } elseif ( $grade >= 70 ) {
        echo "C";
    } else {
        echo "Failed."
    }
Ben
  • 51,770
  • 36
  • 127
  • 149
Strawberry
  • 66,024
  • 56
  • 149
  • 197

7 Answers7

12

This is not answering your actual question but I think you are making a mistake here:

You really shouldn't be thinking about efficiency when using PHP, it is not a language that was designed for speed, but one that was designed for ease of use. Even more so if your application is not yet finished and you haven't verified that this piece of code slows down your whole application (using the profiler of xdebug, for example).

soulmerge
  • 73,842
  • 19
  • 118
  • 155
  • 3
    Jeez I hate when there are answers so good that I feel I must upvote them even if by doing that they will go above mine! +1 :) – Andreas Bonini Feb 03 '10 at 09:36
3

Any such improvements would be micro-optimizations. I think you've got the best solution for both efficiency and clarity.

Andy E
  • 338,112
  • 86
  • 474
  • 445
2

I agree with other posters that you're doing it right already. However, in situations like these you could could try converting the $grade to a value that could be used as an index in an associative array, not unlike what @ghostdog74 tried to do above.

$gradeindex = (int)$grade / 10; // 10 since we want 10-19 = 1, etc..
$gradenames = array('10' => 'A+', '9' => 'A', '8' => B, ..... );

However, since so many of them are identical, I'd probably use a switch()

$gradeindex = (int)$grade / 10; // 10 since we want 10-19 = 1, etc..
switch ($gradeindex) {
  case 10:
  case 9:
    $gradename = 'A';
    break;
  case 8:
    $gradename = 'B';
    break;
  case 7:
    $gradename = 'C';
    break;
  default:
    $gradename = 'Failed';
}
echo $gradename;

But as already said, you're basically best of with your current if statement.

kb.
  • 1,955
  • 16
  • 22
  • Why so many downvotes? It's just an alternative method that might be suitable for other similar scenarios, can I atleast get some comments with the downvotes? – kb. Feb 03 '10 at 10:15
  • 1
    @kb: It looks like a vengeance/competetive downvote to me, most of the other answers here have at least 1 downvote. Sure, it's petty, but it happens all the time and it's best to just ignore it. I would bounce you back with a +1 but I've run out of votes today. – Andy E Feb 03 '10 at 15:31
  • @kb: I understand your point. But I would recommend that you don't take it personally and just ignore it! –  Feb 08 '10 at 21:52
1

I'm sure there are weird ninja ways to do what you're doing, but yours is certainly the best. It's the most clear to read, and performance wise it's too fast to matter.

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
1

Personally, I think I would use a function with multiple returns for this purpose:

function intGradeToStrGrade($grade) {
    if ($grade >= 90) return "A";
    if ($grade >= 80) return "B";
    ...
}

However, there has been some debate here on SO if multiple returns in one function are OK or not. Your choice. I think this is much cleaner than a drawn-out if statement.

Leo
  • 37,640
  • 8
  • 75
  • 100
  • 1
    You are assuming the function doesn't do anything else, such as - for example - opening a `` before echoing the grade and closing it afterwards. – Andreas Bonini Feb 03 '10 at 09:38
  • 3
    Shouldn't be part of the function in clean code anyways, IMO!. – Leo Feb 03 '10 at 09:40
  • I like the idea of a function, and good point to Andreas. I guess it really depends on what exactly is this being applied too. However, what is wrong with the multiple returns in one function you speak of? – Strawberry Feb 03 '10 at 09:41
  • http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement – Leo Feb 03 '10 at 11:52
0

I don't actually know what the efficiency of this is, but I saw this problem and wanted to solve it in a non-nested-if() style so more knowledgeable folk could compare it's relative efficiency. Soooooo... Here's and alternative way of going about it :)

function GetLetterForPercentGrade($grade)
{
    $letter= chr(($grade >59) ? (10-floor($grade /10))+64 : 'F');
    $sign = chr(abs((ceil(((($grade %10)*10)+1)/34)*34)-69)+10);
    return $letter.$sign;
}
Ben Z
  • 11
  • 3
-3
$grade=87;
$grades=array("A"=>range(95,100),"B"=>range(80,94),"C"=>range(70,79),"Failed"=>range(0,69));
foreach($grades as $g=>$v){
    if ( in_array($grade,$v) ){
        print $g."\n";
    }
}
ghostdog74
  • 327,991
  • 56
  • 259
  • 343