The For statement can be said to be the most basic statement of the loop control part of PHP (which is also a multi-lingual language), the execution rule and the basic usage of the For statement are not much said here, see the PHP Manual for statement section. The syntax for this PHP manual is defined as follows:
for (EXPR1; expr2; expr3)
Statement
Here are some useful variants of the for statement.
1. Infinite loop
The first is a well-known infinite loop (also known as a "dead loop"). Because null expressions are syntactically valid, we can leave the three expression of the For statement blank, which results in a continuous execution of the for nested statement.
for (;;) {
Placing statements that need to be executed continuously
}
?>
While there are some tasks that can be used in an infinite loop, most program tasks, especially the areas that PHP can relate to, add some conditions for terminating loops when using an infinite loop.
for (;;) {
If it is ad 2199, jump out of the loop
if (date (' Y ') = = ' 2199 ') {
Break
}
}
?>
2. Using an empty expression
The next thing to say is to use the null syntax in the initialization statement expr1, and the most common function of leaving the empty expr1 is to do more complex initialization work.
if (Isset ($i)) {
Unset ($i);
if ((int) date (') < 2008) {
$i = 0;
} else {
$i = 1;
}
} else {
$i = 3;
}
for (; $i <; $i + +) {
echo $i;
}
?>
In the same vein, the EXPR3 may also be left blank, and can be used to write more complex iterations, such as invoking different iterations based on different conditions.
The conditional statement in the For statement expr2 empty is the above-mentioned infinite loop, of course, you can add some more complex conditions to determine when to jump out of the loop, there is no repetition.
3. Multiple loops
Using multiple loops to control multiple variables is also an attribute that is often ignored in a for statement. As the following example, in general tasks used in general will be a double cycle, more than a triple of the general meaning of the cycle.
for ($i = 0, $j = ten, $i <=, $i + +, $j-) {
echo "$i + $j = 10\r\n";
}
?>
The above code will output:
0 + 10 = 10
1 + 9 = 10
2 + 8 = 10
3 + 7 = 10
4 + 6 = 10
5 + 5 = 10
6 + 4 = 10
7 + 3 = 10
8 + 2 = 10
9 + 1 = 10
10 + 0 = 10
4. More complex expressions
If you write the three expression of a for statement more complex, you can use it to optimize the algorithm. You can even use a for statement without a loop body to accomplish some tasks. For example, calculate summation or factorial:
Calculates the cumulative result of 1-5, bin value to $j
for ($i = 1, $j = 0; $i <= 5; $j + = $i + +);
Echo $j;
Calculates the factorial result of 1-5, the bin value to $j
for ($i = 1, $j = 1; $i <= 5; $j *= $i + +);
Echo $j;
?>
PHP uses the C language syntax to some extent also has C features, such as a powerful for loop statement is a typical example.
http://www.bkjia.com/PHPjc/317210.html www.bkjia.com true http://www.bkjia.com/PHPjc/317210.html techarticle The For statement can be said to be the most basic statement of the loop control part of PHP (which is also a multi-lingual language), the execution rule and the basic usage of the For statement are not much said here, see P ...