Control flow structures allow you to decide which instructions are executed and in what order.
A script that always does the same thing is boring. The power of programming lies in the ability to decide: “If the user is an admin, show them the panel; if not, send them to the login.”
For this we use control structures. We’ll look at the basics, but with a professional approach to prevent your code from turning into an unreadable pyramid.
The classic if / else
The syntax is familiar from almost any language.
$age = 18;
if ($age >= 18) {
echo "You may enter.";
} else {
echo "Go home.";
}The curly braces rule
PHP allows omitting braces if there is only one statement:
// It works, but DON'T do it
if ($age >= 18) echo "Come in";According to PSR-12:
Always use braces {}, even if you only have one line inside.
Why? Because if tomorrow you need to add a second line to the if and forget to add the braces, you will have broken the program’s logic without realizing it. Always be explicit.
The hell of else (and how to avoid it)
One of the biggest beginner mistakes is overusing else. Look at this code (the “Anti-pattern”):
function processPayment($user, $amount) {
if ($user->isActive()) {
if ($amount > 0) {
if ($user->hasBalance($amount)) {
return "Payment successful";
} else {
return "Insufficient balance";
}
} else {
return "Invalid amount";
}
} else {
return "Inactive user";
}
}This is hard to read. You have to keep three levels of conditions in your head to understand what’s happening.
The solution: guard clauses
Instead of nesting inward, check for errors first and exit early. We invert the logic to keep the code flat.
function processPayment($user, $amount) {
// 1. Validations first (Guards)
if (! $user->isActive()) {
return "Inactive user";
}
if ($amount <= 0) {
return "Invalid amount";
}
if (! $user->hasBalance($amount)) {
return "Insufficient balance";
}
// 2. The "Happy Path" at the end, without indentation
return "Payment successful";
}The result is identical, but this code reads like a list of instructions.
The ternary operator (? :)
Sometimes an if/else takes up too much space for something very simple, like assigning a value to a variable.
// Long version
if ($isAdmin) {
$message = "Welcome Boss";
} else {
$message = "Hello user";
}
// Ternary version (Condition ? True : False)
$message = $isAdmin ? "Welcome Boss" : "Hello user";The ternary is fantastic for assignments. Use it to keep your views clean.
Avoid nesting ternary operators: $a ? $b : $c ? $d : $e (This is unreadable and in PHP the associativity can cause surprises). If it’s complex, use an if or match.
The “elvis operator” (?:) vs null coalescing (??)
In the previous lesson we saw the null coalescing operator (??). There is a very similar one called the Elvis Operator (?:), which is a ternary without the middle part.
What is the difference?
- Null Coalescing (
??): Only triggers if the variable isNULL. - Elvis (
?:): Triggers if the variable isFALSE(or evaluates to false, e.g., 0, empty string, null).
Look at the critical difference:
$quantity = 0; // The user bought 0 units
// Using Elvis (?:)
// 0 is considered "false", so it assigns 10. LOGIC ERROR!
$total = $quantity ?: 10; // Result: 10
// Using Null Coalescing (??)
// 0 is NOT null, so it respects the value.
$total = $quantity ?? 10; // Result: 0- Use
??if you want to check if something exists. - Use
?:if you want to check if something has a real value (is not zero or empty).