In the following chapters you will learn how to repeat code by using loops in PHP.
The while loop executes a block of code as long as the specified condition is true.
Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as a certain condition is true.
In PHP, we have the following loop types:
- loops through a block of code as long as the specified condition is truedo...while
- loops through a block of code once, and then repeats the loop as long as the specified
for
- loops through a block of code a specified number of timesforeach
- loops through a block of code for each element in an arrayThe following chapters will explain and give examples of each loop type.
The while
loop executes a block of code as long as the specified condition is true.
The while
loop executes a block of code as long as the specified condition is true.
$i = 1;
while ($i < 6) {
echo $i;
$i++;
}
Note: remember to increment $i
, or else the loop will continue forever.
The while
loop does not run a specific number of times, but checks after each iteration if the condition is still true.
The condition does not have to be a counter, it could be the status of an operation or any condition that evaluates to either true or false.
The break
statement can also be used to jump out of a loop.
Stop the loop when $i
is 3:
$i = 1;
while ($i < 6) {
if ($i == 3) break;
echo $i;
$i++;
}
The continue
statement breaks one iteration (in the loop), if a specified condition occurs, and continues
Stop, and jump to the next iteration if $i is 3:
hile ($i < 6) {
$i++;
if ($i == 3) continue;
echo $i;
}
The while
loop syntax can also be written with the endwhile
statement like this
Print $i
as long as $i
is less than 6:
$i = 1;
while ($i < 6):
echo $i;
$i++;
endwhile;
If you want the while
loop count to 100, but only by each 10, you can increase the counter by 10 instead 1 in each iteration:
$i = 0;
while ($i < 100) {
$i+=10;
echo $i "<br>";
}
The do...while loop
- Loops through a block of code once, and then repeats the loop as long as the specified condition is true.
Print $i
as long as $i
is less than 6:
$i = 1;
do {
echo $i;
$i++;
} while ($i < 6);
Note: In a do...while
loop the condition is tested AFTER executing the statements within the loop. This means that the do...while loop will execute its statements at least once, even if the condition is false. See example below.
Let us see what happens if we set the $i
variable to 8 instead of 1, before execute the same do...while
loop again:
Set $i = 8
, then print $i
as long as $i
is less than 6:
$i = 8;
do {
echo $i;
$i++;
} while ($i < 6);
With the break
statement we can stop the loop even if the condition is still true:
Stop the loop when $i
is 3:
$i = 1;
do {
if ($i == 3) break;
echo $i;
$i++;
} while ($i < 6);
With the continue
statement we can stop the current iteration, and continue with the next:
Continue to the next iteration if $i is 3:
$i = 0;
do {
$i++;
if ($i == 3) continue;
echo $i;
} while ($i < 6);
The loop
- Loops through a block of code a specified number of times.
for (expression1, expression2, expression3) {
// code block
}
This is how it works:
Print the numbers from 0 to 10:
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
The foreach
loop works only on arrays, and is used to loop through each key/value pair in an array.
Loop through the items of an indexed array:
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
echo "$x <br>";
}
In PHP, arrays play a fundamental role in storing and organizing data. The terms "keys" and "values" are often used in the context of arrays:
Print both the key and the value from the $members
array:
$members = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach ($members as $x => $y) {
echo "$x : $y <br>";
}
The foreach
loop can also be used to loop through properties of an object:
Print the property names and values of the $myCar object:
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
}
$myCar = new Car("red", "Volvo");
foreach ($myCar as $x => $y) {
echo "$x: $y <br>";
}
The break
statement can also be used to jump out of a loop.
Stop the loop if $x
is "blue":
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") break;
echo "$x <br>";
}
With the continue
statement we can stop the current iteration, and continue with the next:
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") continue;
echo "$x <br>";
}
The foreach
loop can also be used to modify an array's values:
By default, changing an array item will not affect the original array:
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") $x = "pink";
}
var_dump($colors);
BUT, by using the &
character in the foreach
declaration, the array item is assigned by reference, which results in any changes done to the array item will also be done to the original array:
The foreach
loop syntax can also be written with the endforeach statement like this
Loop through the items of an indexed array:
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) :
echo "$x <br>";
endforeach;
The break
statement can be used to jump out of different kind of loops.
The break
statement can be used to jump out of a for loop.
Jump out of the loop when $x
is 4
:
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
The break
statement can be used to jump out of a while
loop.
$x = 0;
while($x < 10) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
$x++;
}
The break
statement can be used to jump out of a do...while loop.
Stop the loop when $i
is 3
:
$i = 1;
do {
if ($i == 3) break;
echo $i;
$i++;
} while ($i < 6);
The break statement can be used to jump out of a foreach loop.
Stop the loop if $x
is "blue":
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") break;
echo "$x <br>";
}
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
Move to next iteration if $x
= 4:
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
The continue statement can also be used in a while loop (or any other loop).
Move to next iteration if $x
= 4:
$x = 0;
while($x < 10) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
$x++;
}
The continue statement can also be used in a do...while
loop (or any other loop).
Stop, and jump to the next iteration if $i
is 3:
$i = 0;
do {
$i++;
if ($i == 3) continue;
echo $i;
} while ($i < 6);
The continue statement can also be used in a foreach
loop (or any other loop).
Stop, and jump to the next iteration if $x
is "blue":
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") continue;
echo "$x <br>";
}