Strawberry Strawberry - 21 days ago 8
PHP Question

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."
}

Answer

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