New: Complete Beginner's Guide to Coding is now available in Premium
Updated: Indian Govt Exam roadmaps now include salary breakdowns & timelines
Tip: Use the Career Hub to explore all career paths in one place
Tutorials PHP PHP If/Else Conditions

PHP If/Else Conditions

PHP

Conditions let your program make decisions. PHP supports if, else if, else, and switch.

Comparison Operators

  • == Equal to
  • != Not equal
  • > Greater than
  • < Less than
  • >= Greater than or equal
Example — PHP
<?php
$age = 18;

if ($age >= 18) {
    echo "You can vote! ✅";
} else {
    echo "Too young to vote ❌";
}

echo "<br><br>";

$marks = 85;
if ($marks >= 90) {
    echo "Grade: A+";
} elseif ($marks >= 75) {
    echo "Grade: A";
} elseif ($marks >= 60) {
    echo "Grade: B";
} else {
    echo "Grade: C";
}
?>
Result
You can vote! ✅

Grade: A

Interview Questions — PHP If/Else Conditions

5 questions commonly asked in interviews

Q1 What are conditions in PHP? Beginner

Conditions allow code to execute based on certain conditions.

Q2 What is if statement? Beginner

Executes code if condition is true.

Q3 What is else? Beginner

Runs when condition is false.

Q4 What is elseif? Intermediate

Checks multiple conditions.

Q5 What is switch? Intermediate

Handles multiple conditions.

Frequently Asked Questions

Common doubts about PHP If/Else Conditions

Yes.

Depends on use case.

Comparison operator.

Strict comparison.

Yes like && and ||.

Test Your Knowledge

5 questions · Earn 50 XP

0 / 5
Q1 Which is conditional?
Q2 Else runs when?
Q3 Switch is used?
Q4 Which operator is strict?
Q5 Logical AND?

More on PHP If/Else Conditions

Cheatsheet, tips, resources & what to learn next

Quick Cheatsheet

If
if($x > 5){ echo \"Yes\"; }
Else
else { echo \"No\"; }
Elseif
elseif($x == 5){ }
Switch
switch($x){ case 1: break; }
Ternary
$x > 5 ? \"Yes\" : \"No\";

Pro Tips

1

Use === for comparison.

2

Avoid deep nesting.

3

Use switch for multiple values.

4

Keep conditions readable.

5

Use ternary for short logic.

Up Next

PHP Loops PHP Arrays PHP Functions PHP Forms PHP Sessions