A for loop is the most complex loop structure in PHP. Its operations are similar to those in C. The syntax of the for loop is: for (expr1; expr2; expr3) statement the first expression (expr1) is unconditionally evaluated once before the beginning of the loop. Expr2 is opened in each loop
A for loop is the most complex loop structure in PHP. Its actions are similar to those in C language. The syntax of the for loop is:
For (expr1; expr2; expr3)
Statement
The first expression (expr1) is unconditionally evaluated once before the start of the loop.
Expr2 is evaluated before the start of each loop. If the value is TRUE, the nested loop statement is executed continuously. If the value is FALSE, the loop is terminated.
Expr3 is evaluated (executed) after each loop ).
Each expression can be considered null. If expr2 is null, the infinite loop goes down (like C, PHP considers it to be TRUE ). This may not be as useless as imagined, because it is often expected that the break statement is used to end the loop rather than the true value determination using the for expression.
Consider the following example where they all show numbers 1 to 10:
/* Example 1 */
For ($ I = 1; $ I <= 10; $ I ){
Echo $ I;
}
/* Example 2 */
For ($ I = 1; $ I ){
If ($ I> 10 ){
Break;
}
Echo $ I;
}
/* Example 3 */
$ I = 1;
For (;;){
If ($ I> 10 ){
Break;
}
Echo $ I;
$ I;
}
/* Example 4 */
For ($ I = 1; $ I <= 10; echo $ I, $ I );
?>
Of course, the first example seems to be the most normal (or fourth), but users may find it convenient to use null expressions in a for loop in many cases.
PHP also supports the replacement syntax of the for loop with colons.
For (expr1; expr2; expr3 ):
Statement;
...
Endfor;
The for statement is the most basic statement in the loop control part of PHP (also in multiple languages). The Fulfillment rules and basic usage of the for statement are not mentioned here. The syntax of PHP is defined as follows:
For (expr1; expr2; expr3)
Statement
The following describes several useful variants of the for statement.
1. infinite loop
The first is the well-known infinite loop (also known as the "death cycle "). Since null is a valid syntax, we can leave the three expressions in the for statement empty, which will result in the continuous execution of the for nested statement.
For (;;){
// Place the statement to be executed continuously
}
?>
Although some tasks will be applied to an infinite loop, most Program tasks, especially the scope of PHP, will add some conditions to terminate the loop when applying an infinite loop.
For (;;){
// If it is 2199 AD, it will jump out of the loop
If (date ('Y') = '000000 '){
Break;
}
}
?>
2. apply an empty expression