The syntax for the For loop is:
Code to copy code as follows
for (EXPR1; expr2; expr3)
Statement
Here are some useful variants of the for statement.
1. Infinite loop
This is called the cycle of death, no beginning or ending.
for (;;) {
Placing statements that need to be executed continuously
}
?>
The dead loop can also jump out of the loop if you mate with if else
Code to copy code as follows
for (;;) {
If it is ad 2199, then jump out of the loop http://www.hzhuti.com/nokia/n93/
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.
Code to copy code as follows
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 = 10rn";
}
?>
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
If we want to jump out of the loop how to operate, the example
Look at the following example of a multiple loop nesting:
for ($i = 1; $i <=; $i + +) {
for ($j = 1; $j <=; $j + +) {
$m = $i * $i + $j * $J;
echo "$m N
”;
if ($m < | | $m > 190) {
Break 2;
}
}
}
Break 2 jumps out of the double loop, you can experiment with one eye, remove the 2, and get the results completely different. If you do not use parameters, just jump out of this cycle, the first layer of the loop will continue to execute.
Note:
Break is used in the various loops and switch statements mentioned above. His role is to jump out of the current syntax structure and execute the following statement. The break statement can take a parameter n, which indicates the number of layers that jump out of the loop, and if you want to jump out of multiple loops, you can use N to indicate the number of layers that jumped out, or if the default is to jump out of the heavy loop without parameters.
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;
?>
If I want to execute to a place, automatically bring up the current loop to execute--instance
for ($i = 1; $i <=; $i + +) {
if ($i% 3 = = 0 | | $i% 7 = = 0) {
Continue
}
}else{
echo "$i N
”;
}
}
?>
PHP's code fragment is output within 100, can not be divisible by 7 and not divisible by 3 of those natural numbers, the first in the loop with the IF condition statement to determine those can be divisible by the number, and then execute continue; statement, directly into the next loop. The following output statements are not executed.
Note:
Continue is used in the loop structure, and the control program discards the code after the loop continue statement and turns to the next loop. Continue itself does not jump out of the loop structure, just give up this cycle. If you use continue in a non-circular structure, such as in a switch statement, the program will have an error.
Excerpt from PHP development
http://www.bkjia.com/PHPjc/478302.html www.bkjia.com true http://www.bkjia.com/PHPjc/478302.html techarticle the syntax for the For loop is: The code follows the copy code for (EXPR1; expr2; expr3) statement below is a few useful variants of the for statement. 1, Infinite cycle of this also called the cycle of death, no open ...