0% found this document useful (0 votes)
13 views3 pages

Loops in PHP

The document explains the importance of loops in PHP, which are essential for code repetition based on conditions. It describes four types of loops: while, do...while, for, and foreach, detailing their syntax and execution process. Each loop serves the same purpose but has different characteristics and use cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Loops in PHP

The document explains the importance of loops in PHP, which are essential for code repetition based on conditions. It describes four types of loops: while, do...while, for, and foreach, detailing their syntax and execution process. Each loop serves the same purpose but has different characteristics and use cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1)Loops - PHP

In PHP, as in many other programming languages, loops are elements


keys. It is essential to master them to develop effectively. Whether we are developing a
simple website, a rocket or a video game, we need loops.

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

An explanatory part is dedicated to each loop.

Loopwhile

The instructionwhilecould be translated into French as 'tant que'. If we simplify


the use of this loop, we can say that this last one will execute the same portion of
Code waits until the condition is met. Below is an example with the two possible syntaxes.

<?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.

You might also like