The PHP for loop statement USES method details
- 2020-03-31 20:40:04
- OfStack
The for loop is just a little more code, and the loop is added to it. And the common task of one of the cycles involved is:
Sets the initial value of some counter variables.
Please check that the conditional statement is correct.
Loop of code in execution.
The increment passes through the loop counter at the end of each iteration.
The for loop allows you to define these steps in a simple line of code. It seems to have a strange form, so be sure to pay close attention to the grammar used!
The syntax for the for loop is:
For (expr1; Expr2; Expr3)
The statement
The first expression (expr1) is evaluated unconditionally once before the loop begins.
Expr2 is evaluated before each loop begins. If the value is TRUE, the loop continues, executing the nested loop statement. If the value is FALSE, the loop is terminated.
Expr3 is evaluated (executed) after each loop.
Each expression can be empty or contain multiple comma-separated expressions. In expr2, all comma-separated expressions are evaluated, but only the last result is taken. Expr2 being empty means that it will loop indefinitely (PHP, like C, considers it TRUE). This may not be as useful as you might think, because you often want to end the loop with a break statement instead of using the true value of the expression for.
Let's look at simple case one
The output value
The value is now zero
The value is now 1
The value is now 2
In the first loop, $I =0, which means to express, ($i< = 2), is true. Therefore, when the print statement is executed, $I gets plus 1, which becomes 1.
In the second loop, $= 1, which means to express, ($i< = 2), is true. Therefore, when the print statement is executed, $I gets plus 1 and becomes 2.
In the third iteration, $I = 2, which means to express, ($i< = 2), is true. Therefore, when the print statement is executed, $I increments to 1, 3.
In the fourth iteration, $I = 3, which means to express, ($i< = 2), which is false. Therefore, PHP does not execute loops and print statements.
Example 2
The output value
Quantity Price
10 to 50
20, 100
30, 150
40, 200
50, 250
60, 300
70, 350,
80, 400,
90, 450,
100, 500,
See the following examples, which show the Numbers 1 to 10:
Of course, the first example looks the most normal (or the fourth), but users may find it convenient to use empty expressions in the for loop in many situations.
PHP also supports an alternative syntax for for loops with colons.
We often need to iterate over the following arrays:
The problem with the above code is that the second expression of for causes the code to execute slowly -- because the length of the array has to be computed each time the loop is executed. Since the length of the array is always the same, we can use an intermediate variable to store the array length, and then use this variable as the second expression of the for loop. In this way, the value of the variable can be directly used during the loop, without having to recalculate every time. As follows:
Sets the initial value of some counter variables.
Please check that the conditional statement is correct.
Loop of code in execution.
The increment passes through the loop counter at the end of each iteration.
The for loop allows you to define these steps in a simple line of code. It seems to have a strange form, so be sure to pay close attention to the grammar used!
The syntax for the for loop is:
For (expr1; Expr2; Expr3)
The statement
The first expression (expr1) is evaluated unconditionally once before the loop begins.
Expr2 is evaluated before each loop begins. If the value is TRUE, the loop continues, executing the nested loop statement. If the value is FALSE, the loop is terminated.
Expr3 is evaluated (executed) after each loop.
Each expression can be empty or contain multiple comma-separated expressions. In expr2, all comma-separated expressions are evaluated, but only the last result is taken. Expr2 being empty means that it will loop indefinitely (PHP, like C, considers it TRUE). This may not be as useful as you might think, because you often want to end the loop with a break statement instead of using the true value of the expression for.
Let's look at simple case one
FOR ($i = 0; $i <= 2; $i++)
{
print "value is now " . $i . "<br>";
}
The output value
The value is now zero
The value is now 1
The value is now 2
In the first loop, $I =0, which means to express, ($i< = 2), is true. Therefore, when the print statement is executed, $I gets plus 1, which becomes 1.
In the second loop, $= 1, which means to express, ($i< = 2), is true. Therefore, when the print statement is executed, $I gets plus 1 and becomes 2.
In the third iteration, $I = 2, which means to express, ($i< = 2), is true. Therefore, when the print statement is executed, $I increments to 1, 3.
In the fourth iteration, $I = 3, which means to express, ($i< = 2), which is false. Therefore, PHP does not execute loops and print statements.
Example 2
$brush_price = 5;
echo "<table border="1" align="center">";
echo "<tr><th>Quantity</th>";
echo "<th>Price</th></tr>";
for ( $counter = 10; $counter <= 100; $counter += 10) {
echo "<tr><td>";
echo $counter;
echo "</td><td>";
echo $brush_price * $counter;
echo "</td></tr>";
}
echo "</table>";
The output value
Quantity Price
10 to 50
20, 100
30, 150
40, 200
50, 250
60, 300
70, 350,
80, 400,
90, 450,
100, 500,
See the following examples, which show the Numbers 1 to 10:
<?php
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}
$i = 1;
for (;;) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>
Of course, the first example looks the most normal (or the fourth), but users may find it convenient to use empty expressions in the for loop in many situations.
PHP also supports an alternative syntax for for loops with colons.
for (expr1; expr2; expr3):
statement;
...
endfor;
We often need to iterate over the following arrays:
<?php
$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 above code is that the second expression of for causes the code to execute slowly -- because the length of the array has to be computed each time the loop is executed. Since the length of the array is always the same, we can use an intermediate variable to store the array length, and then use this variable as the second expression of the for loop. In this way, the value of the variable can be directly used during the loop, without having to recalculate every time. As follows:
<?php
$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);
}
?>