For statement
You can use a For statement if you have determined how many times the code block has been repeatedly executed.
Grammar
Php
| The code is as follows |
Copy Code |
for (initialization; condition; increment)
{
code to is executed;
}
|
Note: The For statement has three parameters. The first parameter initializes the variable, the second parameter holds the condition, and the third parameter contains the increment required to perform the loop. If you include multiple variables in the initialization or increment parameter, you need to separate them with commas. The condition must evaluate to TRUE or false.
Example
The following example displays the text "Hello world!" 5 times:
Php
| The code is as follows |
Copy Code |
|
<body>
<?php
For ($i =1 $i <=5; $i + +)
{
echo "Hello world!<br/>";
}
?>
</body>
|
Let's take a look at the positive sequence of the 99 multiplication table to use nested loops.
| The code is as follows |
Copy Code |
<?php
/**
99 positive sequence of multiplication table
**/
For ($i =1 $i <=9; $i + +)
{
echo "<br>";
For ($n =1 $n <=9; $n + +)
{
if ($i >= $n)
{
echo "$i * $n". = '. $i * $n;
echo "&NBSP";
}
}
}
?>
|
The results of running this PHP:
The following wants to make this multiplication table reverse, in fact, only for the loop within the number of gradually diminishing.
| The code is as follows |
Copy Code |
<?php
/**
99 the reverse of the multiplication table
**/
for ($i =9; $i >=1; $i-)
{
echo "<br>";
for ($n =9; $n >=1; $n-)
{
if ($i >= $n)
{
echo "$i * $n". = '. $i * $n;
echo "&NBSP";
}
}
}
?>
|
The reverse display is as follows: