PHP Loops
A loop allows a block of code to repeat some number of times. Depending on the type of loop, it can repeat a set number of times, until a particular condition is no longer met, or once for each member of an array. The process of repeating the code block is called iteration.
for Loop
A for loop is used in PHP when it is known how many times its bock of code should execute. The syntax looks like this:
for (init; test; increment) {
code to execute;
}
init: Initialize a counter variable.test: Tests the counter variable. The loop continues to iterate as long as this test evaluates totrue.increment: Increment/decrements the counter variable. Executed on each loop iteration.code to execute: Block of code to execute on each iteration. The counter variable is available inside this block.
The order these pieces are executed are: init, test, execute, increment, then back to test.
Note that it’s possible to write a for loop that never executes if the initial test fails.
Example
<?phpfor ($i = 1;$i <= 10; $i++) {echo 'iteration #' . $i . '<br/>';}?>
Output:
iteration #1iteration #2iteration #3iteration #4iteration #5iteration #6iteration #7iteration #8iteration #9iteration #10
foreach Loop
A foreach loop iterates through a block of code once for each element of an array. The syntax is as follows:
Iterate through array values:
foreach ($array as $value) {
code to execute;
}
$array: An array variable.$value: Variable to receive each value of array in turn.execute: Block of code to execute on each iteration. The$valuevariable is available inside this block.
Iterate through array keys and values:
foreach ($array as $key => $value) {
code to execute;
}
$array: An array variable.$key: Variable to receive each key of array in turn.$value: Variable to receive each value of array in turn.code to execute: Block of code to execute on each iteration. The$keyand$valuevariables are available inside this block.
Examples
Values only:
<?php$a = ['one','two','three','four'];foreach ($a as $x) {echo 'The value is ' . $x . '<br />';}?>
Output:
The value is oneThe value is twoThe value is threeThe value is four
Values and keys:
<?php$a = ['one' => 'green','two' => 'blue','three' => 'red','four' => 'yellow'];foreach ($a as $k => $x) {echo 'The value for ' . $k . ' is ' . $x . '<br />';}?>
Output:
The value for one is greenThe value for two is blueThe value for three is redThe value for four is yellow
while Loop
The while loop iterates through a block of code as long as a condition evaluates to true. The syntax looks like this:
while (test) {
code to execute;
}
test: The expression to evaluate on each iteration of loop. Iterations continue until this evaluates tofalse.code to execute: Block of code to execute on each iteration.
Example
<?php$x = 0;$i = 2;while ($x <= 100) {echo 'the number is ' . $x . '<br/>';$x += $i;$i *= 2;}?>
Output:
the number is 0the number is 2the number is 6the number is 14the number is 30the number is 62
do...while Loop
The do...while loop, like a while loop, iterates as long as a condition is true.
But the do...while loop tests the condition after the iteration, not before, so the loop always executes at least once.
The syntax looks like this:
do {
execute;
} while (test);
test: The expression to evaluate on each iteration of loop. Iterations continue until this evaluates tofalse.execute: Block of code to execute on each iteration.
Example
<?php$x = 125;do {echo 'the number is ' . $x . '<br/>';$x++;} while ($x <= 100);?>
Output:
the number is 125
break Statement
The break statement can be used to prematurely jump out of a loop during execution.
Example
<?php$x = 0;$i = 2;while ($x <= 100) {echo 'the number is ' . $x . '<br/>';if ($x == 14) { break; }$x += $i;$i *= 2;}?>
Output:
the number is 0
the number is 2
the number is 6
the number is 14
continue Statement
The continue statement breaks out of the current loop and immediately starts the next iteration.
Example
<?phpfor ($i = 1;$i <= 10; $i++) {if ($i % 2 == 1) {continue; }echo 'iteration #' . $i . '<br/>';}?>
Output:
iteration #2
iteration #4
iteration #6
iteration #8
iteration #10
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours