Usage of the PHP tutorial loop statement for while doing while
<?php
Loop structure
One, while loop
while (expression)
{
loop body;//Repeat until the expression is false
}
<?php
$num = 1;
while ($num <= 10) {
Print "number is $num <br/>";
$num + +;
}
print ' Done. '
?>
There is a certain difference between a Do While loop statement and a while, and the difference is that a does while a while must be true will execute first, regardless of whether the condition is true.
do {
echo "Mmmmm ... I Love cookies! *munch Munch munch* ";
while ($cookies > 1);
The output is.
Mmmmm ... I Love cookies! *munch Munch Munch
Third, for Loop
Depending on the cycle conditions, there are two types of loops
One: Counting loops (generally used for)
Another: Conditional loops (general use while do-while)
For
(EXPR1; expr2; expr3) {
Statement
}
Where the EXPR1 is the initial value of the condition. EXPR2 is the condition of judgment, which is usually judged by the logic operation symbol (logical operators). EXPR3 the part to be executed after the execution of statement to change the condition for the next round of judgment, such as add one. Wait a minute. and statement for the implementation of the conditions of the program, if the program has only one line, you can omit the curly braces {}.
The following example, written with a For loop, "dare not later", can be taken and compared with a while loop.
<?php
For ($i =1 $i <=10; $i + +) {
echo "$i. <br>n";
}
?>
Output table
<HEAD>
<title>value of Shares</title>
<BODY>
<table border=1>
<?php
for ($shares = 1; $shares <= $shares + +) {
$cost = $shares * 20;
echo "<tr><td>the cost of $shares share/s is $cost #x0024s </TD>", "N";
$dividend = $cost * 0.10;
echo "<td>the dividend is $dividend #x0024s </TD></TR>", "N";
}
?>
</TABLE>
</BODY>
</HTML>
Cumulative calculation
<?php
$count = 1;
while ($count < 5) {
echo "$count squared =". Pow ($count, 2). " <br/> ";
$count + +;
}
?>
Do While loop
<title>the Do...while statement</title>
<body>
<div>
<?php
$num = 1;
do {
Print "Execution number: $num <br/>n";
$num + +;
while ($num > && $num < 400);
?>
</div>
</body>