PHP If...Else...Elseif
Conditional statements allow PHP to make decisions.
What is it?
Conditional statements allow PHP to make decisions.
Why do we use it?
They execute different code depending on a condition.
How does it work?
PHP evaluates conditions from top to bottom and executes the matching block.
Syntax
if ($condition) {
// code
} elseif ($condition) {
// code
} else {
// code
}
Example
<?php
$marks = 72;
if ($marks >= 80) {
echo "Grade A";
} elseif ($marks >= 60) {
echo "Grade B";
} else {
echo "Needs Improvement";
}
?>
Example Explained
More Examples
<?php $age = 20; if ($age >= 18) echo "Eligible"; else echo "Not Eligible"; ?>
⚠️ Important Rules
Arrange overlapping conditions carefully.
❌ Common Mistakes
Incorrect comparison operators or wrong condition order.
🌍 Real-World Use
Grades, permissions, validation and business rules.
📝 Practice Exercise
Create a student grade calculator.
💻 Try It Yourself
Write and run this PHP lesson inside the AIFA Coding Lab.