Introduction to PHP

Conditionals (if / else)

if checks a condition and runs code only when it's true. else runs when it's false, and elseif checks another condition in between. Conditions almost always use comparison or logical operators inside the parentheses.

Why this matters

  • ·Lets a script make decisions instead of always doing the same thing
  • ·elseif chains handle many cases cleanly

Common mistakes

  • ·Missing curly braces around a multi-line if block causes only the first line to run conditionally
  • ·Forgetting elseif and stacking separate if statements can run more than one block by mistake
Your codePHP
Expected output
Grade: B

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Frequently asked questions

Use the code editor above edit the example freely and check the expected output against your own reasoning.

Practice questions

Question 1

Write an if/elseif/else chain that prints "Pass" for marks 40 or above, and "Fail" otherwise.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Quick check

Just for this topic answer honestly, then check yourself.

1 / 1

Which keyword checks another condition after an if fails?