Php loop control statement. In php, we usually use the following four types of loop statements: while, dowhile, for (), and foreach, which are commonly used in development, if you have any need, you can refer to the following four types of loop statements in php: while, do while, for (), and foreach, it is also a loop statement that is commonly used in development. if you need it, you can refer to it. this tutorial describes the usage of this statement one by one.
Foreach statement
The Foreach loop is introduced in php4.0 and can only be used as an array. In php5, object support is added. The syntax format of this statement is:
Foreach (array_expression as $ value)
Statement;
Or
The code is as follows: |
|
Foreach (array_expression as $ key => $ value) Statement; FOREACH ($ array_variable as $ value) { [Code to execute] }
Or FOREACH ($ array_variable as $ key => $ value) { [Code to execute] } |
In these two cases, multiple [code executions] will be executed equal to the number of elements in the $ array_variable array.
Let's look at an example. Suppose we have the following code segment:
The code is as follows: |
|
$ Array1 = array (1, 2, 3, 4, 5 ); FOREACH ($ array1 as $ abc) { Print "new value is". $ abc * 10 ." "; } Output result New value is 10 New value is 20 New value is 30 New value is 40 New value is 50 |
The foreach loop goes through the array $ array1 of all five elements, and each time a declaration containing 10 times the value of the array element is printed.
Foreach traverses all values of the current array and assigns them to $ var.
Let's look at a foreach multi-dimensional data operation instance.
The code is as follows: |
|
$ S = array (1, 2), array (3, 4), array (5, 6 )); Foreach ($ s as $ v =>$ _ v) { Foreach ($ _ v as $ vc =>$ _ vc) { Echo $ _ vc [0], '|'. $ _ vc [1],' '; // Print_r ($ _ vc ); } } |
For more details, see http://www.bKjia. c0m/phper/18/foreach-foreach.htm
For statement
Best traversal
The code is as follows: |
|
/* 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, $ j = 0; $ I <= 10; $ j + = $ I, print $ I, $ I ++ ); ?> |
Traverse arrays
The code is as follows: |
|
/* * This is an array with some data we want to modify * When running through the for loop. */ $ People = Array ( Array ('name' => 'Kalle', 'Salt' => 856412 ), Array ('name' => 'Pierre ', 'Salt' => 215863) ); For ($ I = 0; $ I <sizeof ($ people); ++ $ I) { $ People [$ I] ['Salt'] = rand (000000,999 999 ); } ?> |
NextWhile and do while
While loop is the simplest loop statement in php. Its syntax format is:
The code is as follows: |
|
While (expression ){ Statement; }
|
When the expression value is true, the statement is executed. after the statement is executed, return to the expression for further judgment. The loop does not appear until the expression value is false.
The code is as follows: |
|
/* Example 1 */ $ I = 1; While ($ I <= 10 ){ Echo $ I ++;/* the printed value wocould be $ I before the increment (Post-increment )*/ } /* Example 2 */ $ I = 1; While ($ I <= 10 ): Echo $ I; $ I ++; Endwhile; ?> |
Do... While statement
The While statement also has a form of representation, Do... While. syntax:
Do {
Statement;
} While (expression );
The difference between the two is: Do... The While statement has more than one loop than the While statement.
When the While expression value is false, the While loop directly jumps out of the current loop, While Do... The While statement first executes the program block and then judges the expression.
Instance
The code is as follows: |
|
Do { If ($ I <5 ){ Echo "I is not big enough "; Break; } $ I * = $ factor; If ($ I <$ minimum_limit ){ Break; } Echo "I is OK "; /* Process I */ } While (0 ); ?> |
When while, for (), foreach, these four types are also commonly used loop statements in development. if you need them, you can refer to this article...