Iterative loops are designed to repeat a task as long as a condition is met or until the loop is explicitly chosen to exit..
Example
Output
Example
FOR can also be used successfully if we have several variables that have successively incremented numerical suffixes; for example, if we have
The output will be:
Example
Example
The output is the same in all three cases:
WHILE
1 2 3 4 |
WHILE (expr) { ECHO... or $variab=...; } |
1 2 3 4 5 6 7 |
<?php $nr=1; WHILE ($nr<=3) { ECHO "Number ". $nr."<br>"; $nr++; } |
Number 1
Number 2
Number 3
FOR
1 2 3 4 |
FOR ($expr_init; $expr_condit; $expr_de_modific) { ECHO... or $variab=...; } |
1 2 3 4 5 6 |
<?php FOR($i=1; $i<=3; $i++) { ECHO "<pre>Number " . $i . "</pre>"; } ?> |
$variab1, $variab2, $variab3
, etc., ${"variab$i"}
can be used.
1 2 3 4 5 6 7 8 |
<?php // For example, for variables $variab1, $variab2, $variab3, $variab4, $variab5 $variab1="ONE"; $variab2="TWO"; $variab3="THREE"; $variab4="FOUR"; $variab5="FIVE"; FOR($i = 1; $i <= 5; $i++) { ECHO "<pre>Number " .${"variab$i"}. "</pre>"; } ?> |
1 2 3 4 5 |
Number ONE Number TWO Number THREE Number FOUR Number FIVE |
FOREACH
1 2 3 4 |
FOREACH ($array AS $val) { ECHO... or $variab=...; } |
1 2 3 4 5 6 7 |
<?php $numere = ARRAY(1, 2, 3); FOREACH($numere AS $val) { ECHO "<pre>Number " . $val . "</pre>"; } ?> |
DO WHILE
1 2 3 4 5 |
DO { ECHO... or $variab=...; } WHILE (expr); |
1 2 3 4 5 6 7 8 9 |
<?php $i = 0; DO { $i++; ECHO "<pre>Number " . $i . "</pre>"; } WHILE($i <= 3); ?> |
1 2 3 |
Number 1 Number 2 Number 3 |
Use of consecutively numbered items
In order to make it convenient to use FOR, when we use elements that are numbered (as we have exemplified above), the root of that element can be extracted and concatenated with $i.
So for $img1, $img2...
we use ${"img$i"}
.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $cale="https://www.w3schools.com/howto/"; $img1="img_nature.jpg"; $img2="img_fjords.jpg"; $img3="img_mountains.jpg"; $img4="img_woods.jpg"; FOR($i=1; $i <= 4; $i++) { ?> <img src="<?php ECHO $cale.${"img$i"}; ?>" /> <?php } ?> |
If we look in the page source, we will see as follows:
1 2 3 4 |
<img src="https://www.w3schools.com/howto/img_nature.jpg" /> <img src="https://www.w3schools.com/howto/img_fjords.jpg" /> <img src="https://www.w3schools.com/howto/img_mountains.jpg" /> <img src="https://www.w3schools.com/howto/img_woods.jpg" /> |
One way to set the maximum that the counter variable, $i
, can have is by using an array. Thus, instead of:
1 |
FOR($i=1; $i <= 4; $i++) |
we will have:
1 2 |
$imag=[$img1, $img2, $img3, $img4]; FOR($i=1; $i <= count($imag); $i++) |
Break/Continue
Break
can be used to exit a loop.. Continue
breaks an iteration when a condition occurs and continues with the next iteration in the loop..
Break
Example
1 2 3 4 5 6 7 8 9 |
<?php FOR ($nr = 1; $nr < 10; $nr++) { IF ($nr== 4) { BREAK; } ECHO "Number $nr<br>"; } |
Output
Number 1
Number 2
Number 3
Continue
Example
1 2 3 4 5 6 7 8 9 10 |
<?php FOR ($nr = 1; $nr <= 5; $nr++) { IF ($nr == 3) { CONTINUE; } ECHO "Numărul $nr <br>"; } ?> |
Output
Number 1
Number 2
Number 4
Number 5
- Meloni, J., Învaţă singur, PHP, MySQL şi Apache. Toate într-o singură carte, Ed. Corint, Bucureşti, 2005, p. 100.
- W3Schools.com