0% complete

Conditionals · Lesson 3 of 6

elseif

Test several mutually exclusive conditions in order.

Lesson

An elseif branch is considered only when earlier conditions were false. Order ranges from most specific to least specific so a broad condition does not hide a more specific case. Include a final else when every remaining value needs a deliberate default. The example maps a validated score to a grade band. Further guidance Test each branch with a normal value, a boundary value, and an unexpected value. Order range checks from specific to broad, make the fallback behavior intentional, and keep related decisions readable. A condition should use validated data rather than an untrusted request value directly. Practice task: Create a fresh example for elseif. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.

Code examples

elseif
PHP
$score = 84;
if ($score >= 90) {
    echo 'A';
} elseif ($score >= 80) {
    echo 'B';
} else {
    echo 'Keep practicing';
}
Expected output
B

Key points

  • Test several mutually exclusive conditions in order.
  • Test boundary cases for every conditional branch.
  • Keep fallback behavior explicit and use validated values.

Your learning progress

Finish this lesson

Optional practice

PHP playground

This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.

Execution is disabled. No code is sent to a server.