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 loop

The 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

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
 {
 要执行的代码;
 }
 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

QQ截图20161008150201.png 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
   //定义循环的初始值
  $i=0;
  echo '<table width="800" border="1">';
   
   
  while($i<100){
          //输出列0-99的列了
      echo '<td>'.$i.'</td>';
          //一定要加哟,不然死循环了
          $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的时候,应该显示行开始的标签
    if($i%10 == 0){
                //为了隔行变色,每20,40,60每行的颜色是PHP学院的,因此我们又可以再进行一次取余运算
        if($i%20==0){
            echo '<tr>';
        }else{
            echo '<tr bgcolor="pink">';
        }
    }
 
    echo '<td>'.$i.'</td>';
 
    $i++;
        //同理,每一行结束是不是应该有一个</tr>结束标签呢?
    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.

Continuing Learning
||
<html> <body> <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br>"; $i++; } ?> </body> </html>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!