Control Flow in PHP
Learn conditionals, loops, and decision-making in PHP with if/else, match, for, while, and foreach using Docker-ready examples
Control flow is how a program decides what to do next. Instead of running every line top to bottom, control flow lets PHP branch on conditions, repeat work, and short-circuit logic. These constructs are the backbone of every PHP request that renders a page, validates a form, or loops over database rows.
As a multi-paradigm language with dynamic, weak typing, PHP gives you the familiar C-style toolkit — if/elseif/else, for, while, do-while, switch, and break/continue — alongside modern additions like the match expression (PHP 8.0) and the null coalescing operator (??). Because PHP is weakly typed, conditions coerce values to booleans following specific “truthiness” rules, which is worth understanding early.
In this tutorial you’ll work through conditionals, the match expression, every loop type, loop control, and the concise ternary and null-coalescing operators. Each example is a complete, runnable script.
Conditionals: if, elseif, else
The if statement runs a block only when a condition evaluates to true. PHP coerces the condition to a boolean, so 0, 0.0, "", "0", null, and empty arrays are all “falsy.”
Create a file named conditionals.php:
| |
The match Expression
Introduced in PHP 8.0, match is a modern alternative to switch. It uses strict comparison (===), returns a value, and requires no break statements. Unlike switch, it does not fall through.
Create a file named match_expression.php:
| |
Loops: for, while, do-while
PHP supports the classic C-style loops. for is ideal when you know the iteration count, while checks its condition before each pass, and do-while always runs at least once.
Create a file named loops.php:
| |
Iterating with foreach
The foreach loop is PHP’s idiomatic way to walk arrays — both indexed (list-style) and associative (key/value maps). It’s the loop you’ll reach for most often in real PHP code.
Create a file named foreach_loop.php:
| |
Loop Control: break and continue
break exits a loop entirely, while continue skips to the next iteration. Both accept an optional level argument to control nested loops.
Create a file named loop_control.php:
| |
Ternary and Null Coalescing
For concise decisions, PHP offers the ternary operator (?:) and the null coalescing operator (??), which returns the first operand that exists and is not null.
Create a file named concise_conditions.php:
| |
Running with Docker
| |
Running Locally
If you have PHP installed:
| |
Expected Output
Running conditionals.php:
Grade: B
Falsy: the string "0" is considered false
Entry granted
Running match_expression.php:
Status 404: Not Found
Temperature 30 is Warm
Running loops.php:
for loop: 1 2 3 4 5
while loop: 10 8 6 4 2
do-while loop: run(0) run(1) run(2)
Running foreach_loop.php:
Language: PHP
Language: Python
Language: Rust
PHP is at version 8.4
Python is at version 3.13
Rust is at version 1.85
Running loop_control.php:
Odd numbers: 1 3 5 7 9
Searching: 1 2 3 found 4, stopping
Running concise_conditions.php:
Account: Empty or overdrawn
Welcome, Guest
Theme: dark, Language: en
Key Concepts
- Weak typing affects conditions — PHP coerces condition values to booleans, so
0,"","0",null, and empty arrays are all falsy. The string"0"being falsy is a classic gotcha. matchis the modernswitch— It uses strict (===) comparison, returns a value, has no fall-through, and needs nobreak. Use it instead ofswitchin PHP 8+.foreachis the workhorse loop — It cleanly iterates both indexed and associative arrays, with optional$key => $valuedestructuring.continueandbreakshape loops —continueskips the current iteration;breakexits the loop. Both accept a numeric level for nested loops.??vs?:— The null coalescing operator (??) only triggers onnullor undefined values and won’t emit a warning for missing array keys; the Elvis operator (?:) triggers on any falsy value.- Choose the right loop — Use
forfor counted iterations,whilefor condition-driven loops,do-whilewhen the body must run at least once, andforeachfor collections.
Running Today
All examples can be run using Docker:
docker pull php:8.4-cli-alpine
Comments
Loading comments...
Leave a Comment