Loops in PHP
Understand all loop types in PHP — for, while, do-while, and foreach — and learn how to use break and continue to control iteration.
for Loop
The `for` loop is ideal when you know in advance how many times to iterate. It bundles the initializer, condition, and increment into one concise header.
1. Basic for Loop
`for (init; condition; increment)` — runs while condition is true.
2. Counting Down
Decrement `$i` to iterate in reverse.
3. Custom Step
Increment by any amount using compound assignment.
4. Nested for Loops
Use nested loops for 2D iteration like tables or matrices.
while Loop
The `while` loop checks its condition before each iteration. Use it when the number of iterations is not known in advance — for example, reading lines from a file or polling until a result is ready.
1. Basic while
Loop runs while the condition is truthy.
2. Condition-driven while
Drive the loop with a changing condition, not just a counter.
3. while with Array Consumption
Use while to process items until a collection is empty.
do-while Loop
The `do-while` loop executes its body first, then checks the condition. This guarantees the body runs at least once — regardless of whether the condition is initially true.
1. Basic do-while
Body executes before the condition is checked.
2. Runs Once Even When Condition is False
Unlike `while`, `do-while` always executes the body at least once.
3. Menu / Retry Pattern
do-while is natural for "prompt once, repeat if needed" patterns.
foreach Loop
The `foreach` loop is PHP's most-used loop for iterating over arrays and objects. It automatically handles the index/key and value for each element.
1. foreach on Indexed Array
Iterate over values of an indexed array.
2. foreach with Key => Value
Access both key and value in an associative array.
3. Modifying Values with Reference (&)
Use `&$value` to modify array elements in place.
4. Nested foreach (Multidimensional Array)
Loop over nested arrays with nested foreach.
break & continue
PHP provides two keywords to alter loop execution: `break` exits the loop entirely, and `continue` skips the rest of the current iteration and moves to the next one.
1. break — Exit the Loop
Stop a loop immediately when a condition is met.
2. continue — Skip an Iteration
Jump to the next iteration without finishing the current one.
3. break with a Level Argument
Pass an integer to `break` to exit multiple nested loops at once.
4. break in foreach (Early Exit Search)
Use break to stop a search once a result is found.
Loop Guard Clauses
A guard clause inside a loop uses `continue` to reject invalid or unwanted items early, keeping the main logic flat and readable instead of nested inside an `if` block.
1. Guard Clause using continue
Skip invalid items at the top of the loop body.
2. Guard Clause with Multiple Conditions
Combine multiple guards at the top to filter out bad data.
Loop Best Practices
Small choices in how you write loops — like avoiding recalculating values in loop headers or choosing foreach over for for arrays — can meaningfully improve clarity and performance.
1. Cache count() Outside the Loop
Calling `count()` inside a loop header re-evaluates it every iteration.
2. Prefer foreach for Arrays
Use foreach instead of for when iterating over arrays.
3. Avoid Modifying an Array While Iterating
Modifying the array inside a foreach can produce unexpected behavior.