Thuta Learning
IntermediateProgrammingbeginner

If...Else...Elseif

Relax. We'll talk through this in plain words — no textbook voice.

if, elseif, else are used to run different code blocks depending on a condition — for example, checking whether a user is logged in, whether stock is available, whether someone's old enough, or whether a form is valid.

php
<?php
$score = 82;

if ($score >= 90) {
  echo "Grade A";
} elseif ($score >= 80) {
  echo "Grade B";
} elseif ($score >= 60) {
  echo "Grade C";
} else {
  echo "Try again. Practice is the real cheat code.";
}
?>
You should see
Grade B

Easy traps

  • Getting the order of your conditions wrong creates a logic bug — no error shows up, but the result comes out wrong. It's a silent villain.