Using elseif StatementsIn fact, even more is built into the if statement. You can also use elseif statements to check other conditions. Say, for example, that you want to tell students what their grades were on a recent test using a handy PHP script. If you were restricted to using if and else statements, your script might look like this, where you could only report whether a student got an A or not: <?php $test_score = 78; if ($test_score > 90) { echo "You got an A!"; } else { echo "You didn't get an A."; } ?> The elseif statement can come to the rescue here. This statement is much like an else statement because it's only executed if the main if condition was falsebut you can include a new condition to test in each elseif statement. Here's how that would work for checking whether the student got an A, B, C, D, or...uh oh...: <?php $test_score = 78; if ($test_score > 90) { echo "You got an A!"; } elseif ($test_score > 80) { echo "You got a B."; } elseif ($test_score > 70) { echo "You got a C."; } elseif ($test_score > 60) { echo "You got a D."; } else { echo "Uh oh."; } ?> Here's what you'd see from this script: You got a C. Very coolnow you can test many conditions in the same if statement. |