while and do...while statements
PHP Loop - While Loop
Loops through a block of code a specified number of times, or when a specified condition is true.
PHP Loops
When you write code, you often need to have the same block of code run over and over again. We can use loop statements in our code to accomplish this task.
In PHP, the following loop statements are provided:
·??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????. Execute a code block, and then repeat this cycle when the specified conditions are established
# · For-The number of times specified by the circular execution code block
· FOREACH-according to each element in the array
while loopThe while loop will repeatedly execute a block of code until the specified condition is not true.
Syntax
while (condition)
{ Code to be executed;
}
Example
The following example first Set the value of variable i to 1 ($i=1;).
Then, as long as i is less than or equal to 5, the while loop will continue to run. Each time the loop runs, i will be incremented by 1:
<html> <body> <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br>"; $i++; } ?> </body> </html>
Output:
The number is 1
The number is 2 The number is 3
The number is 4
The number is 5
do...while statement will execute the code at least once and then check the condition, as long as the condition Once established, the cycle will repeat.
Syntax
do { 要執(zhí)行的代碼; } while (條件);
Example
The following example first sets the value of variable i to 1 ($i=1;).
Then, start the do...while loop. The loop increments the value of variable i by 1 and then outputs it. First check the condition (i is less than or equal to 5), as long as i is less than or equal to 5, the loop will continue to run:
<html> <body> <?php $i=1; do { $i++; echo "The number is " . $i . "<br>"; } while ($i<=5); ?> </body> </html>
Output:
The number is 2
The number is 3 The number is 4
The number is 5
The number is 6
Then we have to write a 0-99 now A table with alternating rows of colors.
Define the initial value, output the table label and the column label in the table
<?php //定義循環(huán)的初始值 $i=0; echo '<table width="800" border="1">'; while($i<100){ //輸出列0-99的列了 echo '<td>'.$i.'</td>'; //一定要加喲,不然死循環(huán)了 $i++; } echo '</table>'; ?>
2. Add the logic generated by the row
<?php $i=0; echo '<table width="800" border="1">'; while($i<100){ //0 - 9 為一行 //10 -19 為一行 //因此,每一行都能夠被10求默,如為為10的時候,應(yīng)該顯示行開始的標(biāo)簽 if($i%10 == 0){ //為了隔行變色,每20,40,60每行的顏色是PHP學(xué)院的,因此我們又可以再進行一次取余運算 if($i%20==0){ echo '<tr>'; }else{ echo '<tr bgcolor="pink">'; } } echo '<td>'.$i.'</td>'; $i++; //同理,每一行結(jié)束是不是應(yīng)該有一個</tr>結(jié)束標(biāo)簽?zāi)兀? if($i%10==0){ echo '</tr>'; } } echo '</table>'; ?>
Note: Do not write an infinite loop (no exit condition loop)
whie(1){
echo 1111.'<br />';}
do...while statement
Do-while is very similar to while loop, the difference is that the value of the expression is checked at the end of each loop instead of at the beginning. The main difference from a regular while loop is that the do-while loop statement is guaranteed to be executed once (the truth value of the expression is checked after each loop), but this is not necessarily the case in a regular while loop (the truth value of the expression Checked at the beginning of the loop, if it is FALSE at the beginning, the entire loop terminates immediately).
The do-while loop has only one syntax: <?php
$i = 0;
do {
echo $i;
} while ($i > 0 );
?>
The above loop will run exactly once, because after the first loop, when the truth value of the expression is checked, its value is FALSE ($i is not greater than 0) and Causes the loop to terminate.
There is a certain difference between the Do While loop statement and while. The difference between them is that do while will be executed first regardless of whether the condition is true, while while must be true before it will be executed once.