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!
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.
See simple example one
Copy CodeThe code is as follows:
for ($i = 0; $i <= 2; $i + +)
{
Print "value is now". $i. "
";
}
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.
Example Two
Copy CodeThe code is as follows:
$brush _price = 5;
echo "
"; echo "
Quantity | "; echo "
| Price
"; for ($counter = ten; $counter <=; $counter + =) {echo]
"; Echo $counter; echo " |
"; echo $brush _price * $counter; echo " |
"; } echo "
";
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:
Copy CodeThe code is as follows:
/* Example 1 */
for ($i = 1; $i <=; $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 <=; $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.
Copy CodeThe code is as follows:
for (EXPR1; expr2; expr3):
Statement
...
ENDfor;
We often need to iterate over an array such as the following:
Copy CodeThe code is as follows:
/*
* We want to change the values of some elements in the following array during traversal
*/
$people = Array (
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:
Copy CodeThe code is as follows:
$people = Array (
Array (' name ' = ' Kalle ', ' salt ' = 856412),
Array (' name ' = ' Pierre ', ' salt ' = 215863)
);
for ($i = 0, $size = sizeof ($people); $i < $size; + + $i)
{
$people [$i] [' salt '] = rand (000000, 999999);
}
?>
http://www.bkjia.com/PHPjc/321822.html www.bkjia.com true http://www.bkjia.com/PHPjc/321822.html techarticle The For loop is just a little bit more code, and the loop is added to it at the same time. The common task of a cycle involved is to set the initial value of some counter variables. Please check that the conditional statement is ...