Control Flow in PHP
In any real application, your code needs to make decisions — showing different outputs based on conditions like user input, login status, or data values. This is where control flow comes in.
In this lesson, you’ll learn how to control the execution of your PHP code using if, else, switch, and the modern match statement. By the end, you’ll be able to write decision-based logic for real-world applications.
What is Control Flow in PHP?
Control flow determines how your program executes based on conditions. Instead of running every line of code, PHP evaluates conditions and decides which block should run.
The if Statement
The if statement runs a block of code only if a condition is true.
Syntax
if (condition) {
// code to execute
}
Example
$age = 20;
if ($age > 18) {
echo "You are eligible";
}
Output:
You are eligible
Use case:
Used when you want to execute code only if a specific condition is met.
The if...else Statement
Use else when you want to handle the opposite condition.
$age = 16;
if ($age >= 18) {
echo "Adult";
} else {
echo "Minor";
}
Output:
Minor
Use case:
Handling two possible outcomes, such as pass/fail or logged in/not logged in.
The if...elseif...else Statement
Use this when you have multiple conditions.
$marks = 75;
if ($marks >= 90) {
echo "Grade A";
} elseif ($marks >= 70) {
echo "Grade B";
} else {
echo "Grade C";
}
Output:
Grade B
Use case:
Used in grading systems, pricing tiers, or multi-level decisions.
The switch Statement
The switch statement is used when you want to compare a single variable against multiple values.
Example
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of week";
break;
case "Friday":
echo "Weekend coming";
break;
default:
echo "Regular day";
}
Output:
Start of week
Use case:
Cleaner than multiple elseif conditions when checking one variable against many values.
The match Expression (Modern PHP)
The match expression (introduced in PHP 8) is a more concise and strict alternative to switch.
Example
$day = "Monday";
$result = match ($day) {
"Monday" => "Start of week",
"Friday" => "Weekend coming",
default => "Regular day"
};
echo $result;
Output:
Start of week
Why match is Better than switch
- Uses strict comparison (
===) - No need for
break - Returns a value directly
- Cleaner and less error-prone
Key Difference: switch vs match
Feature | switch | match --------------- | ------------ | -------------- Comparison type | Loose (==) | Strict (===) Break required | Yes | No Returns value | No | Yes Syntax | Verbose | Clean
Common Mistakes Beginners Make
1. Missing Curly Braces
if ($age > 18)
echo "Adult";
Why it’s risky:
Only the next line is controlled, which can lead to bugs in larger blocks.
2. Forgetting break in switch
case "Monday":
echo "Start";
Why it fails:
Execution continues to next case (fall-through bug).
3. Using switch for Complex Conditions
switch ($age > 18) {
Why it’s wrong:
switch is meant for fixed values, not expressions.
4. Expecting Loose Comparison in match
match ("10") {
10 => "Number"
};
Why it fails:
match uses strict comparison, so this will not match.
Real-World Example
$isLoggedIn = true;
$userRole = "admin";
if ($isLoggedIn && $userRole === "admin") {
echo "Welcome Admin";
} else {
echo "Access Denied";
}
Output:
Welcome Admin
What this shows:
- Logical operators (
&&) - Comparison (
===) - Conditional execution
Practice Exercise
Task 1
Check if a number is greater than 50.
Expected Output:
Greater
Task 2
Use if...else to check if a user is logged in.
Expected Output:
Logged In
Task 3
Use switch or match to print day message.
Expected Output:
Start of week
Summary
In this lesson, you learned how control flow works in PHP using if, else, switch, and match. These structures allow your program to make decisions and execute different code based on conditions.
Mastering control flow is essential for building dynamic applications like login systems, dashboards, and user-driven features.