Loop Structure in PHP

Loop structure in php - The loop structure is a program instruction that aims to repeat several command lines. In designing the looping of the program code, we should at least know the 3 components, the initial conditions of the loop, the program command to be repeated, and the final conditions in which the loop will stop.
The Loop Structure For
for (init_early, condition, counter) {
Loop statement;
}

Information :
  • init_early is the initialization or initial value of the variable.
  • Condition is a condition statement that will limit the iteration.
  • Counter is an increase or decrease in the value of the variable so that the loop is still running.
The Loop Structure While
init_early;
while (condition) {
Loop statement;
counter;
}

Information :
  • init_early is the initialization or initial value of the variable.
  • Condition is a condition statement that will limit the iteration.
  • Counter is an increase or decrease in the value of the variable so that the loop is still running.
The Loop Structure Do ... while
init_early;
do {
Loop statement;
counter;
} while (
condition);
Information :
  • init_early is the initialization or initial value of the variable.
  • Condition is a condition statement that will limit the iteration.
  • Counter is an increase or decrease in the value of the variable so that the loop is still running.
  • In the do ... while structure, the condition checks are below, so the statements that are in the block do ... while at least will be executed as much as once.
The Loop Structure Foreach
foreach (array_expression as $value)
statement;
foreach (array_expression as $key => $value)
statement;

Information :
  • The foreach structure is usually used to loop based on the contents of an array. The loop ends when the array contents are exhausted.
In order to better understand the structure of the loop, here I give examples of implementation in a program.
Loop Structure in PHP
Program 1
File Name: for.php
Description: The Fores Structure Program and some variations.
Open notepad then type the following php code.
<?php

/* example 1 */
for ($i = 1; $i <= 10; $i++) {
echo "$i ";
}
echo "<br><br>";

/* example 2 */
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo "$i ";
}
echo "<br><br>";

/* example 3 */
$i = 1;
for (; ; ) {
if ($i > 10) {
break;
}
echo "$i ";
$i++;
} echo "<br><br>";

/* example 4 */
for ($i = 1; $i <= 10; print "$i ", $i++);

?>

Loop Structure in PHP

Save the code into the htdocs folder, name it for.php.
Program Description 1
The above program is a form of iteration using for. The first example (lines 4-6) is the most common form. In example 2, the end loop is not mentioned in for, but is set with if and break (lines 11-13).
To see the result, open the browser then in the address bar type http://localhost/for.php then enter. The result will look like the following.
Loop Structure in PHP
Program View

Program 2
File Name: while.php
Description: Temporary Loop Structure Program and some variations.
Type the following code in notepad.
<?php
/* example 1 */
$i = 1;
while ($i <= 10) {
echo $i++;
}
echo "<br><br>";

/* example 2 */
$i = 1;
while ($i <= 10):
echo "$i";
$i++;
endwhile;
echo "<br><br>";

/* example 3 */
$i = 1;
while ($i <= 6) {
echo "<h$i>Heading $i</h$i>";
$i++;
}
?>

Loop Structure in PHP

Save the code in the htdocs folder with the while.php name.
Program Explanation 2
The above program is some variation of looping with while. The first example (lines 3-6) is the most commonly used form. The command block (line 5) will run during checking condition on line 4 is TRUE. The second example is another form of while, this form is rarely used. In the third example, the use of iteration to display posts with the format <H1> to <H6>.
To see the result open the browser, then in the address bar type http://localhost/while.php. Then the result will look like the following.
Loop Structure in PHP
Program View

Program 3
File Name: dowhile.php
Description: Program Structure Recurrence with Do ... While.
Open notepad then type the following code into notepad
<?php

$i = 1;
do {
echo "$i ";
$i+=2;
} while ($i <= 20);

?>

Loop Structure in PHP

Save the code into the htdocs folder named dowhile.php.
Program Description 3
The above program will display an odd number between 1 and 20 using the do..while iteration structure. In the do ... while loop structure, the condition checks are below.
To see the results, open the browser type http://localhost/dowhile.php in the address bar. Then the result will look like the following.
Loop Structure in PHP
Program View

That's our article about Loop structure in php. Hopefully the above explanation can increase our knowledge in understanding the php programming language. Read also an article on Explain Conditional Structure in PHP.

Comments