if-else and switch in PHP
In this lesson: Use if, else, elseif and switch in PHP — grade calculator and menu examples with output in Hindi & English.
Why we need decisions
A program often has to choose what to do based on a value. Should it show "Pass" or "Fail"? Is the user an adult or a minor? These choices are made with conditional statements — if, elseif, else and switch. They let the code take different paths for different situations.
$age >= 18.The if statement
if runs a block of code only when its condition is true.
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult";
}
?>
If the condition is false, PHP simply skips the block and moves on.
if - elseif - else
Add else for the "otherwise" case, and elseif for extra conditions in between.
<?php
$marks = 75;
if ($marks >= 90) {
echo "Grade A";
} elseif ($marks >= 60) {
echo "Grade B";
} else {
echo "Grade C";
}
?>
PHP checks the conditions top to bottom and runs the first one that is true, then skips the rest. Since 75 is not ≥ 90 but is ≥ 60, "Grade B" runs.
The switch statement
When you compare one variable against several exact values, switch is cleaner than many elseifs.
<?php
$day = 3;
switch ($day) {
case 1: echo "Monday"; break;
case 2: echo "Tuesday"; break;
case 3: echo "Wednesday"; break;
default: echo "Other day";
}
?>
Each case ends with break so only the matching one runs. default handles anything that did not match.
Short if (ternary)
For a simple either/or, the ternary operator does it in one line.
<?php
$age = 16;
$msg = ($age >= 18) ? "Adult" : "Minor";
echo $msg;
?>
Read it as: condition ? value-if-true : value-if-false. Great for short decisions, but for anything complex a full if-else is more readable.
Common mistakes
- Using
=instead of==.if ($x = 5)assigns and is always true; use==. - Forgetting
breakin switch. Without it, later cases also run (fall-through). - Wrong condition order. Put stricter conditions first, or a looser one may catch the value early.
- Missing curly braces. Always use
{ }so it is clear which lines belong to the if.
Summary
ifruns code when a condition is true;elsehandles the rest.elseifadds more conditions; PHP runs the first true one.switchcompares one variable to exact values — usebreakin each case.- The ternary
? :is a one-line if-else for simple choices. - Always use
==/===in conditions, not=.
Next, repeat actions with loops in PHP, or review PHP operators used inside conditions.
Frequently Asked Questions
What is the difference between if-elseif and switch?
if-elseif when you test different conditions or ranges (like marks > 90). Use switch when you compare one variable against several exact values (like a day number 1 to 7). switch is cleaner for many equal-value checks.What is the difference between = and == in a condition?
= assigns a value, == compares values. Inside an if you almost always want == (or ===). Writing if ($x = 5) by mistake assigns 5 and is always true — a classic bug.Why do we use break in a switch?
break stops the switch once a matching case runs. Without it, PHP keeps running the next cases too (called fall-through), which is usually not what you want. Add break at the end of each case.What does the ternary operator do?
$msg = ($age >= 18) ? "Adult" : "Minor"; means: if age is 18 or more, msg is "Adult", otherwise "Minor". It keeps short decisions compact.Can I nest if statements inside each other?
elseif or combining conditions with && and || is clearer.