Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More efficient way of performing multiple IFs

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."
    }
like image 315
Strawberry Avatar asked Feb 03 '10 09:02

Strawberry


2 Answers

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).

like image 111
soulmerge Avatar answered Oct 10 '22 23:10

soulmerge


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

like image 3
Andy E Avatar answered Oct 11 '22 00:10

Andy E