The meaning of the while statement is simple, and it tells PHP to repeatedly execute the loop statements in the nested if the value of the while expression is TRUE. The value of the expression is checked every time the loop is started, so even if the value changes in the Loop statement, the statement does not stop executing until the end of the loop. Sometimes a loop statement does not execute once if the value of the while expression starts with FALSE.
Loop structure: Repeat the operation according to the stipulated conditions, pay attention to the stopping condition, otherwise it is prone to die cycle.
1.while loop, satisfies the condition, then executes the loop body repeatedly
While expression {
Loop body
}
<?php$i=0;while ($i <7) { $i + +; echo $i, ' <br> ';}
Output: 1 2 3 4 5 6 7
2.do...while cycle, first execute once again to make judgment
do{
EXECUTE statement
}while-expression
<span style= "FONT-SIZE:18PX;" ><?php$i=7;do{ $i + +; echo $i;} while ($i <7);</span>
Output: 8
3.for Cycle
for (initial value; conditional expression; increment) {
Loop body
}
<?phpfor ($i =0; $i <10; $i + +) { echo ' Hello World ', ' <br> ';}
4. Several loops-related statements
Break stop and exit, BREAK2, BREAK3 respectively exit 2 layer loop, exit 3 layer Loop
<?php$i=0;do{ $i + +; echo $i; if ($i ==4) {break ; }} while ($i <7);
Output: 1 2 3 4
Continue skip this cycle
<?php$i=0;do{ $i + +; if ($i%2==0) { continue; } echo $i;} while ($i <10);
Output 1 3 5 7 9