The for statement can be said to be the most basic statement in the loop control part of PHP (and multiple languages). The execution rules and basic usage of the for statement are here Not much to say, you can refer to the PHP manualfor statement section. Its syntax is defined in the PHP manual as follows:
for (expr1; expr2; expr3)
statement
Let’s talk about several useful variations of the for statement.
1. Infinite Loop
The first is the infinite loop (also called "dead loop") that everyone knows. Since the empty expression null is syntactically valid, we can leave the three expressions of the for statement empty, which will have the effect of continuously executing nested for statements.
for (;;) {
//Place statements that need to be executed continuously
}
?>
Although some tasks will use infinite loops , but most program tasks, especially the areas that PHP can involve, will add some conditions to terminate the loop when using infinite loops.
for (;)
) == '2199'
) { break; The most common purpose of leaving expr1 blank is to complete more complex initialization work. if (isset($i
)) {
unset(
$i
); if ((int )
date
(''
) < 2008) { else {
; }} else {
$i =3;}for (;$i
< 10;$ i
++) {
echo $i;}?> It is also possible to leave xpr3 blank, or Use this to write more complex iterations, such as calling different iterations based on different conditions.
Leaving the conditional statement expr2 in the for statement blank is the infinite loop mentioned above. Of course, you can also add some more complex conditions to determine when to jump out of the loop, which will not be repeated here.
3. Multiple loops
Using multiple loops to control multiple variables is also a feature that is often overlooked in the for statement. As in the example below, double loops are generally used in general tasks, and loops of three or more are generally of little significance.
2 + 8 = 103 + 7 = 104 + 6 = 105 + 5 = 106 + 4 = 107 + 3 = 10
8 + 2 = 10
9 + 1 = 1010 + 0 = 104. More complex expressions If the three expressions of the for statement are written more complicated, then Can be used for optimization algorithms. You can even use a for statement without a loop body to complete some tasks. For example, calculate the accumulation or factorial:
//Calculate the accumulation result of 1-5, and the value is equal to $j
for (
$i
=
1
,
$j =
0; $i <= 5;$j += $i ++);echo $j;//Calculate the factorial result of 1-5, bin value to $jfor ($i
= 1,
$j =
1; $i <= 5;$j *= $i++); echo $j ;?> PHP uses the syntax of C language and will also have C features to a certain extent. For example, the powerful for loop statement is a typical example.
About the author:
lm92 is one of the members of the PHP Chinese documentation team: Liu Ming, graduated from high school this summer and is now attending a university in Guangdong His blog http://blog.donews .com/phpor/
The above has introduced several variations of the for loop statement in for your entertainment PHP, including the content of for your entertainment. I hope it will be helpful to friends who are interested in PHP tutorials.