The meaning of for in PHP
The For loop is the most complex loop structure in PHP. Its behavior is similar to the C language. The number of times to execute the same code set in PHP
The For loop is just a little bit more code, and the loop is added to it at the same time. And the common task of a cycle involved is:
Sets the initial value of some counter variables.
Please check that the condition statement is correct.
The code loop in execution.
The increments end at each iteration through the loop counter.
The For loop allows you to define a simple line of code for these steps. It seems to have a strange form, so be sure to pay close attention to grammar use!
How do I write the For statement in PHP?
The syntax for the For loop is:
for (EXPR1; expr2; expr3)
Statement
The first expression (EXPR1) is evaluated unconditionally once before the loop begins.
The EXPR2 is evaluated before each cycle begins. If the value is TRUE, the loop continues and the nested loop statement is executed. If the value is FALSE, the loop is terminated.
The EXPR3 is evaluated (executed) after each loop.
Each expression can be empty or include multiple expressions separated by commas. In expression expr2, all expressions separated by commas are evaluated, but only the last result is taken. Expr2 means an infinite loop (like C, PHP considers its value to be TRUE). This may not be as useless as you might think, as it is often desirable to end a loop with a break statement instead of using the for expression truth.
PHP for Loop statement simple instance One
The code is as follows:
for ($i = 0; $i <= 2; $i + +) {print ' value is ' now '. $i. "<br>"; }
Output value
Value is now 0
Value is now 1
Value is now 2
In the first loop, $i = 0, which means expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i gets added 1, which becomes 1.
In the second loop, $ = 1, which means expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i gets added 1, which becomes 2.
In the third iteration, $i = 2, which means expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i increments, which becomes 1 3.
In the fourth iteration, $i = 3, which means expression, ($i <= 2), is false. Therefore, PHP does not execute loops and does not execute print statements.
PHP for Loop statement Simple example Two
The code is as follows:
$brush _price = 5; echo "<table border=" 1 "align=" center ">"; echo "<tr><th>Quantity</th>"; echo "<th>Price</th></tr>"; for ($counter = ten; $counter <=, $counter + +) {echo "<tr><td>"; echo $counter; echo "</TD>&L T;td> "; echo $brush _price * $counter; echo "</td></tr>"; } echo "</table>";
Output value
Quantity Price
10 50
20 100
30 150
40 200
50 250
60 300
70 350
80 400
90 450
100 500
You can refer to the following examples, which show numbers 1 through 10:
The code is as follows:
<?php/* Example 1 */for ($i = 1; $i <=, $i + +) {echo $i,}/* Example 2 */for ($i = 1;; $i + +) {if ($i > 1 0) {break;} echo $i; }/* Example 3 */$i = 1; for (;;) {if ($i >) {break;} echo $i; $i + +;} /* Example 4 */for ($i = 1, $j = 0; $i <=, $j + = $i, print $i, $i + +);?>
Of course, the first example looks the most normal (or fourth), but the user may find it convenient to use an empty expression in a for loop in many situations.
PHP also supports substitution syntax for a for loop with colons.
The code is as follows:
for (EXPR1; expr2; expr3): statement; ... endfor;
We often need to iterate over an array such as the following:
The code is as follows:
<?php/* We want to change the value of some elements in the following array during traversal */$people = array (' name ' = ' Kalle ', ' salt ' = 856412), array (' name ' = ' Pierre ', ' salt ' = 215863); for ($i = 0; $i < sizeof ($people), + + $i) {$people [$i] [' salt '] = rand (000000, 999999);}?>
The problem with the code above is that the second expression of for will cause the code to execute very slowly-because the length of the array is evaluated once each loop. Since the length of the array is always constant, we can use an intermediate variable to store the length of the array, and then use that variable as the second expression for the For loop. This allows you to use the value of the variable directly at the time of the loop, without having to recalculate it every time. As follows:
The code is as follows:
<?php $people = Array (' name ' = ' Kalle ', ' salt ' = ' 856412 '), Array (' name ' = ' Pierre ', ' salt ' = 2158 63)); for ($i = 0, $size = sizeof ($people), $i < $size, + + $i) {$people [$i] [' salt '] = rand (000000, 999999);}?>