Control Flow in PHP
What is Control Flow?
Control flow refers to the order in which your code's statements are executed. By default, PHP runs code line by line, from top to bottom. Control flow structures let you break out of that straight line — making decisions, skipping sections, or choosing between different paths depending on conditions in your data.
Almost every real program needs to make decisions: Is the user logged in? Is the cart empty? Is the age above 18? Control flow statements are how PHP answers questions like these and decides what to do next.
The if Statement
The most basic control flow structure is if, which runs a block of code only when a condition evaluates to true.
php
<?php
$age = 20;
if ($age >= 18) {
echo "You are eligible to vote.";
}
Here, the code inside the curly braces only runs because $age >= 18 evaluates to true. If the condition were false, PHP would simply skip that block entirely and move on.
Adding else and elseif
Real decisions usually involve more than one possible outcome. That's where else and elseif come in.
php
<?php
$marks = 72;
if ($marks >= 90) {
echo "Grade: A";
} elseif ($marks >= 75) {
echo "Grade: B";
} elseif ($marks >= 60) {
echo "Grade: C";
} else {
echo "Grade: F";
}
PHP checks each condition in order, from top to bottom, and runs the first block whose condition is true. If none of the conditions match, the else block runs as a fallback. Only one block ever executes — once a match is found, PHP skips the rest.
Nesting Conditions
Conditions can be placed inside other conditions when a decision depends on more than one factor:
php
<?php
$isLoggedIn = true;
$isAdmin = false;
if ($isLoggedIn) {
if ($isAdmin) {
echo "Welcome, Admin.";
} else {
echo "Welcome, User.";
}
} else {
echo "Please log in.";
}
Nesting is useful, but deeply nested conditions can become hard to read. Combining conditions using logical operators (like && and ||) is often a cleaner alternative:
php
<?php
if ($isLoggedIn && $isAdmin) {
echo "Welcome, Admin.";
}
The switch Statement
When a single variable needs to be checked against many possible fixed values, a long elseif chain can get repetitive. The switch statement is designed for exactly this situation:
php
<?php
$day = "Tuesday";
switch ($day) {
case "Monday":
echo "Start of the work week.";
break;
case "Tuesday":
echo "Second day of the week.";
break;
case "Saturday":
case "Sunday":
echo "It's the weekend!";
break;
default:
echo "A regular weekday.";
}
A few important details about switch:
- Each
caseis checked against the value using loose comparison (==), not strict comparison. - The
breakkeyword stops execution from "falling through" into the next case. Forgettingbreakis one of the most common bugs beginners run into withswitch. - Multiple
caselabels can share the same block, as shown with Saturday and Sunday above. defaultacts as the fallback, similar toelse.
The match Expression (Modern PHP)
PHP 8 introduced match, a more modern alternative to switch that's stricter, more concise, and returns a value directly:
php
<?php
$day = "Tuesday";
$message = match ($day) {
"Monday" => "Start of the work week.",
"Tuesday" => "Second day of the week.",
"Saturday", "Sunday" => "It's the weekend!",
default => "A regular weekday.",
};
echo $message;
Unlike switch, match uses strict comparison (===) by default, doesn't require a break, and can be assigned directly to a variable. For simple value-matching logic, match is generally the cleaner, more modern choice in PHP 8+.
Comparing if/elseif, switch, and match
Featureif / elseifswitchmatchComparison typeAny expressionLoose (==)Strict (===)Needs break?NoYesNoReturns a value?No (needs extra code)No (needs extra code)Yes, directlyBest forComplex or range-based conditionsLegacy-style multi-value checksClean, modern value matching
Ternary and Null Coalescing Shortcuts
For very simple conditions, PHP offers shorthand syntax that avoids writing a full if block:
php
<?php // Ternary operator $status = ($age >= 18) ? "Adult" : "Minor"; // Null coalescing operator $username = $_GET['user'] ?? "Guest";
The ternary operator is useful for simple either/or assignments, while the null coalescing operator (??) is commonly used to provide a default value when a variable might not be set — a very frequent situation when working with form data or arrays.
What's Next
Control flow lets your programs make decisions, but most real applications also need to repeat actions — validating multiple form fields, processing lists of data, or displaying items from a database. That's exactly what loops are for, which is the focus of the next lesson.