In any programming language, repetition loops are used to repeat part of the code. PHP loop, like all languages, to repeat the code. In PHP, there are different types of repetition loops, which we will introduce below.
All iteration loops are executed until the loop condition is true.
Types of PHP Loop
- for
- foreach
- while
- do…while
for loop
We use the “for” loop when we know the number of repetitions of the loop in advance.
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
- init counter: Defines a variable that will store the number of iterations.
- test counter: It is used to evaluate loop repetition. If the condition is true, the loop is repeated, and if the condition is false, the execution of the loop ends.
- increment counter: specifies the number of increments of the variable.
for ($counter = 0; $counter <= 10; $counter++) {
echo "The number is: $counter <br/>";
}
----------------
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
This example prints the numbers 0 to 10. First, a variable named counter is defined and its initial value is set to 0. In the second step, because the value of the counter (0) is less than 10, the codes inside the loop are executed. Meanwhile, the counter value changes from 0 to 1. Then code execution returns to “for”. Because the value of the variable (1) is less than 10, then the codes inside the loop are executed. This process continues until the value of the counter reaches 11, and since it is not less than 10, the execution of the codes inside the loop is stopped.
This example prints numbers with a step of 1.
for ($counter = 0; $counter <= 10; $counter += 2) {
echo "The number is: $counter <br/>";
}
------------------
The number is: 0
The number is: 2
The number is: 4
The number is: 6
The number is: 8
The number is: 10
This example also prints numbers with step 2. Each time the loop is executed, 2 units are added to the counter.
Advanced: Multiple variables can be used inside a “for” loop.
for ($x = 0, $y = 20; $x <= 10, $y >= 10; $x++, $y--) {
echo "The number is: x = $x and y = $y <br/>";
}
---------------------
The number is: x = 0 and y = 20
The number is: x = 1 and y = 19
The number is: x = 2 and y = 18
The number is: x = 3 and y = 17
The number is: x = 4 and y = 16
The number is: x = 5 and y = 15
The number is: x = 6 and y = 14
The number is: x = 7 and y = 13
The number is: x = 8 and y = 12
The number is: x = 9 and y = 11
The number is: x = 10 and y = 10
foreach loop in PHP
This loop is like the “for” loop, except that this loop is used to execute code based on the data of an array.
foreach ($array as $value) {
code to be executed;
}
For each iteration of the loop, the value of the current array element is assigned to the $value and the array pointer is moved by step one until it reaches the last element of the array.
$cars = array("Ferrari", "Benz", "BMW", "Volvo");
foreach ($cars as $value) {
echo "$value <br/>";
}
-----------------
Ferrari
Benz
BMW
Volvo
In this loop, the value of the array key can also be used.
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo",
);
foreach ($cars as $key => $value) {
echo "$key = $value <br/>";
}
--------------
fer = Ferrari
ben = Benz
bmw = BMW
vol = Volvo
while loop
This loop will be executed like the “for” loop until the while condition is true.
while (condition is true) {
code to be executed;
}
$counter = 1;
while($counter <= 5) {
echo "The number is: $counter <br/>";
$counter++;
}
---------------------
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
In this example, the numbers 1 to 5 are printed. As you can see, its function is almost similar to the “for” loop.
As mentioned, this loop is executed until the condition is true. Therefore, it is possible to create an infinite loop with this loop.
$counter = 1;
while(true) {
echo "The number is: $counter <br/>";
$counter++;
}
This code will be executed infinitely many times because the condition is always true.
do…while loop
This loop is like a while loop except that the code inside it is executed at least once even if the condition is false. In fact, this loop checks the condition after the code is executed.
do {
code to be executed;
} while (condition is true);
$counter = 1;
do {
echo "The number is: $counter <br/>";
$counter++;
} while ($counter <= 5);
------------------------
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
Now we set the initial value of the counter variable to 6.
$counter = 6;
do {
echo "The number is: $counter <br/>";
$counter++;
} while ($counter <= 5);
----------------
The number is: 6
In this example, the code inside the loop is executed once, and then the condition of the loop is checked. Because the value of 6 is greater than 5, the loop execution does not continue.
Break and Continue in PHP Loop
These commands are used to control the continuation or stopping of loops.
break command
This command is used to stop the execution of the codes inside the loop.
for ($counter = 0; $counter <= 10; $counter++) {
if ($counter == 5) {
break;
}
echo "The number is: $counter <br/>";
}
-------------------
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The execution of codes continues until the value of the counter variable reaches 5, and then the execution of the loop stops.
continue command
This command stops the execution of the codes inside the loop after it.
for ($counter = 0; $counter <= 10; $counter++) {
if ($counter == 5) {
continue;
}
echo "The number is: $counter <br/>";
}
------------------------
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
As you can see, the number 5 is not printed.
Nested PHP loops
Sometimes it is necessary to use a loop inside another loop, these types of loops are called nested loops.
for ($x = 0; $x < 3; $x++) {
for ($y = 0; $y < 3; $y++) {
echo "The number is: x = $x and y = $y <br/>";
}
}
------------------
The number is: x = 0 and y = 0
The number is: x = 0 and y = 1
The number is: x = 0 and y = 2
The number is: x = 1 and y = 0
The number is: x = 1 and y = 1
The number is: x = 1 and y = 2
The number is: x = 2 and y = 0
The number is: x = 2 and y = 1
The number is: x = 2 and y = 2
As you can see in the example, the echo code is executed three times for each y, which is executed three times for x in total 9 times.