Loop
When writing code, you often need to run the same code block multiple times. You can use loop statements in the code to complete this task. In PHP, we can use the following loop statement:
While
The code block is executed cyclically as long as the specified condition is true.
Do... while
Execute a code block first, and then repeat the loop when the specified condition is set.
For
The number of times the code block is executed cyclically.
Foreach
Loop code Blocks Based on each element in the array
While statement
As long as the specified condition is true, the while statement repeats the code block.
Syntax
while (condition)code to be executed; |
Example
The following example demonstrates a loop. As long as the variable I is less than or equal to 5, the code will continue to run cyclically. Every cycle of a loop increases by 1:
<?php $i=1;while($i<=5) { echo "The number is " . $i . " "; $i++; }?> |
Do... while statement
The do... while statement will execute the code at least once-then, as long as the condition is true, the loop will be repeated.
Syntax
do{code to be executed;}while (condition); |
Example
In the following example, the I value is accumulated once. Then, as long as the I is less than 5, it will continue to accumulate:
For statement
If you have determined the number of times the code block is repeated, you can use the for statement.
Syntax
for (initialization; condition; increment){ code to be executed;} |
Do... while statement
The do... while statement will execute the code at least once-then, as long as the condition is true, the loop will be repeated.
Syntax
do{code to be executed;}while (condition); |
Example
In the following example, the I value is accumulated once. Then, as long as the I is less than 5, it will continue to accumulate:
<?php $i=0;do { $i++; echo "The number is " . $i . " "; }while ($i<5);?> |
For statement
If you have determined the number of times the code block is repeated, you can use the for statement.
Syntax
for (initialization; condition; increment){ code to be executed;} |
Note:The for statement has three parameters. The first parameter initializes the variable, the second parameter saves the condition, and the third parameter contains the increment required for the execution cycle. If the initialization or increment parameter contains multiple variables, separate them with commas. The condition must be calculated as true or false.
Example
In the following example, the text "Hello World! "Display 5 times:
<?phpfor ($i=1; $i<=5; $i++){ echo "Hello World! ";}?> |
Foreach statement
The foreach statement is used to traverse arrays cyclically.
For each loop, the value of the current array element is assigned to the value variable (the array pointer is moved one by one), and so on.
Syntax
foreach (array as value){ code to be executed;} |
Example
The following example demonstrates a loop that outputs the value of a given array:
<?php$arr=array("one", "two", "three");foreach ($arr as $value){ echo "Value: " . $value . " ";}?> |