PHP Loops
Loops рдПрдЙрдЯреИ рдХреЛрдб рдмреНрд▓рдХрд▓рд╛рдИ рдкрдЯрдХ-рдкрдЯрдХ execute рдЧрд░реНрди рдкреНрд░рдпреЛрдЧ рдЧрд░рд┐рдиреНрдЫ, рдЬрдмрд╕рдореНрдо рдПрдЙрдЯрд╛ рдирд┐рд╢реНрдЪрд┐рдд рд╕рд░реНрдд (condition) true рд░рд╣рдиреНрдЫред
PHP рдорд╛, рд╣рд╛рдореАрд╕рдБрдЧ рдирд┐рдореНрди loops рд╣рд░реВ рдЫрдиреН:
while- рдЬрдмрд╕рдореНрдо рддреЛрдХрд┐рдПрдХреЛ condition true рд╣реБрдиреНрдЫ, рддрдмрд╕рдореНрдо рдХреЛрдб execute рдЧрд░реНрдЫредdo...while- рдХреЛрдбрд▓рд╛рдИ рдПрдХ рдкрдЯрдХ execute рдЧрд░реНрдЫ, рд░ рддреНрдпрд╕рдкрдЫрд┐ condition true рднрдПрд╕рдореНрдо рджреЛрд╣реЛрд░реНрдпрд╛рдЙрдБрдЫредfor- рддреЛрдХрд┐рдПрдХреЛ рд╕рдВрдЦреНрдпрд╛ (specific number of times) рд╕рдореНрдо рдХреЛрдб execute рдЧрд░реНрдЫредforeach- array рдХрд╛ рд╣рд░реЗрдХ elements рдХрд╛ рд▓рд╛рдЧрд┐ рдХреЛрдб execute рдЧрд░реНрдЫред
PHP `while` Loop
Example
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
PHP `do...while` Loop
Example
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
PHP `for` Loop
Example
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
PHP `foreach` Loop
Example
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>