The number of times to execute the same code set is used in PHP. The basic syntax of the author is as follows:
The For loop is just a little bit more code, and loops are added to it. And one of the common tasks involved in the cycle is:
Sets the initial values of some counter variables.
Please check that the conditional statement is correct.
The code loop in execution.
The increment ends at the end of 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 the grammar!
for (expression 1, expression 2, expression 3)
{
[Code to execute]
}
For (initialize a counter; conditional statement; increment a counter) {
do this code;
}
Look at a simple example of a
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 cycle, $i = 0, which means expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i is added by 1 and becomes 1.
In the second cycle, $ = 1, which means an expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i is added by 1 and becomes 2.
In the third iteration, $i = 2, which means expression, ($i <= 2), for ture. Therefore, when the print statement executes, the $i increments to 1 3.
In the fourth iteration, $i = 3, which means the expression, ($i <= 2), is false. Therefore, PHP does not execute loops and does not execute print statements.
Example Two
$brush _price = 5;
echo "<table border=" 1 "align=" center ">";
echo "<tr><th>Quantity</th>";
echo "<th>Price</th></tr>";
for ($counter = $counter <= $counter + + 10) {
echo "<tr><td>";
Echo $counter;
echo "</td><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
Article Original in Www.111cn.net