Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 07

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.

💡 A condition is anything that is either true or false, like $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";
  }
?>
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";
  }
?>
Grade B

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";
  }
?>
Wednesday

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;
?>
Minor

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 break in 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

  • if runs code when a condition is true; else handles the rest.
  • elseif adds more conditions; PHP runs the first true one.
  • switch compares one variable to exact values — use break in 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?
Both choose one path out of many. Use 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?
It is a one-line shortcut for a simple if-else. $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?
Yes, you can put an if inside another if for more detailed decisions. But too much nesting becomes hard to read — often elseif or combining conditions with && and || is clearer.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicLoops in PHP Your progress: 7 / 50 (14%) · Lesson 8 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free