views:

144

answers:

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."
    }
+1  A: 

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
A: 
$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
This is not equivalent.
Andreas Bonini
That would only work for scores that are exactly 90, 80, etc.
Andy E
+3  A: 

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

Andy E
+12  A: 

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
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
Interesting, and thanks for the extra resource +1
Doug
+1  A: 

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.

Mef
You are assuming the function doesn't do anything else, such as - for example - opening a `<b>` before echoing the grade and closing it afterwards.
Andreas Bonini
Shouldn't be part of the function in clean code anyways, IMO!.
Mef
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?
Doug
http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement
Mef
+2  A: 

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
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
@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
@kb: I understand your point. But I would recommend that you don't take it personally and just ignore it!