PHP CHAPTER 15

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

PHP Syntax
if ($condition) {
    // code
} elseif ($condition) {
    // code
} else {
    // code
}
💻

Example

PHP Example
<?php

$marks = 72;

if ($marks >= 80) {
    echo "Grade A";
} elseif ($marks >= 60) {
    echo "Grade B";
} else {
    echo "Needs Improvement";
}

?>
🔍

Example Explained

PHP first checks 80 or above. If false, it checks 60 or above. Otherwise the else block runs.
🚀

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.

Open Coding Lab →