Loops in PHP
Loops in PHP
Loops allow for the repetition of a portion of code a certain number of times.
based on a condition test. As long as this is verified, then the code portion
executes. For an execution of a portion of code, we speak of an iteration of the loop.
Whatever type of loops is used, it will be necessary to use a condition to
end the loop.
In PHP, there are 4 different loops. They serve the same purpose but have
differences.
while
do...while
for
foreach
Loopwhile
<?php <?php
$z = 0; $z = 0;
while($z < 10) { while($z < 10) :
The variable $z is The variable $z is
as value.<br /> as value.<br />
$z++; $z++;
} endwhile;
?> ?>
Result Result
The variable $z has 0 as its value. The variable $z has a value of 0.
The variable $z has a value of 1. The variable $z has a value of 1.
The variable $z has a value of 2. The variable $z has a value of 2.
The variable $z has a value of 3. The variable $z has a value of 3.
The variable $z has a value of 4. The variable $z has a value of 4.
The variable $z has a value of 5. The variable $z has a value of 5.
The variable $z has a value of 6. The variable $z has a value of 6.
The variable $z has a value of 7. The variable $z has a value of 7.
The variable $z has a value of 8. The variable $z has a value of 8.
The variable $z has a value of 9. The variable $z has a value of 9.
2. Loopdo...while
The instructiondo...whilecould be translated into French as 'do... as long as'. The loop
do...whileis very close to the loopwhileThe difference is that the loopdo...while
starts by executing its portion of code before checking its condition while the loop
whileDo the opposite. An example to compare the two and everything will become clearer.
<?php <?php
$z = 2; $z = 2;
do { while($z < 1) {
The variable $z is The variable $z is '.$z.'
as value. as value.
} while($z < 1); }
?> ?>
Result Result
The variable $z has a value of 2.
In the example on the left withdo...while, the loop starts by executing its once.
code portion. The string is displayed and then the condition test is performed. In
the occurrence of the variable$zdoes not have a value strictly less than 1, the loop therefore stops.
While in the example on the right withwhilethe loop begins by evaluating its test for
condition; nothing is displayed because the variable$zis not strictly less than 1.
3. For loop
As previously stated, all loops have the same purpose: to repeat a portion.
in loop code. We saw previously the loopswhileanddo...while. They
produce the same result but differ from one another by their own process. The
loopforis another type of loop. It is widely used because it has advantages
undeniable, but with a rather particular syntax, especially for beginners. First of all,
the instructionforcould be translated into French as 'for'. Let's take the same example again.
with the loopwhileto highlight the differences. In PHP, the loopfora
two possible syntaxes.
<?php <?php
for($z = 0; $z < 10; $z++) { for($z = 0;$z < 10;$z++) :
The variable $z is The variable $z is
as value.<br /> as value.<br />
} endfor;
?> ?>
Result Result
The variable $z has a value of 0. The variable $z has a value of 0.
The variable $z has a value of 1. The variable $z has a value of 1.
The variable $z has a value of 2. The variable $z has a value of 2.
The variable $z has a value of 3. The variable $z has a value of 3.
The variable $z has a value of 4. The variable $z has a value of 4.
The variable $z has a value of 5. The variable $z has a value of 5.
The variable $z has a value of 6. The variable $z has a value of 6.
The variable $z has a value of 7. The variable $z has a value of 7.
The variable $z has a value of 8. The variable $z has a value of 8.
The variable $z has a value of 9. The variable $z has a value of 9.
4. Foreach loop
The loopforeachis another type of loop. It is useful for array type variables.
(tables). Since we have not yet covered tables, we will cover them.
this loop at the same time as this theme in the next chapter.