More

# Lecture Notes

### Course: Web Development Masterclass

#### Lecture: PHP ElseIf Statement

In this lesson we will explore a more complex set of conditions.

Say for example we wanted to create a grading system that converts a students test score, out of 100 marks, into a letter grade.

First, let's define our grading system:

• < 50 is F
• 50 to 59 D
• 60 - 69 C
• 70 - 79 B
• 80 - 89 A
• => 90 A+

Now we can start creating our if statements.

<!DOCTYPE html>
<html>
<body>

<?php

\$x = 48;

if (\$x < 50) {
echo "F";
} else if (\$x >= 50 && \$x < 60) {
echo "D";
} else if (\$x >= 60 && \$x < 70) {
echo "C";
} else if (\$x >= 70 && \$x < 80) {
echo "B";
} else if (\$x >= 80 && \$x < 90) {
echo "A";
} else {
echo "A+";
}

?>

</body>
</html>

We start with if and create our first condition. if variable x is less then 50...then output F.

Next you will see a series of elseif statements. An elseif statement is used if there are more than two conditions.

In this case there are six conditions.

• The second condition, states that if x is > 50 and x is less than 60 output a D.
• Next, if x is >= 60 but less than 70 output a C.
• if x >= 70 but less then 80 output B.
• if x is >=80 and less then 90 output A.
• We finish of with an else statement, which basically instructs our script to put an A+ in any other scenario. Since the test was out of 100, the only other scenario can be that the mark was equal to or greater than 90 - in which case we output an A+..

You can test different responses using alternate values for x.