PHP While Loop

PHP Loops - 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 make the same block of code run again and again Repeat the run. We can use loop statements in our code to accomplish this task.

There are always some regular repetitive operations in program development

We need to make a block of code execute repeatedly, for example: Table);

• Perform the same calculation (array traversal) on a set of data;

• Execute again after the operation fails...

Loops are composed of loop bodies It consists of a loop condition and a loop body. The loop body represents the code that needs to be executed repeatedly, and the loop condition represents the basis for loop termination.

In PHP, the following loop statements are provided: If the condition is true, then the code block will be executed in a loop. Block a specified number of times

·        foreach - loops a block of code over each element in the array

##while loop

The while loop will repeatedly execute the block of code until the specified condition is not true.

The most common loop in PHP is structurally the same as the if statement. It also depends on a condition

while (expr) statement

is different What's more, the if statement executes the statement once only when expr is true, while the while statement executes the statement repeatedly as long as expr is true.

Usually we also recommend using {} to wrap the statement

$num = 1;

while ($num <= 5) {

echo $num;

}

Syntax

while (condition )

{

Code to be executed;
}

Then, here comes the problem. According to the design of while, if expr is always true, then the loop will execute forever, so we need Change the result of expr in the loop body.

$num = 1;

while ($num <= 5) {

echo $num;
$num++;
}

$num will automatically increase by 1 each time the loop is executed. How many times will the loop be executed?

5 times




Example

The following example first sets 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 is true, it will be repeated Make a loop.

do {

statement

} while (expr);

do...The biggest difference between while loop and while loop is that the detection of expr is to put At the end of the code block, that is to say, regardless of whether expr is true or not, the do...while loop will be executed at least once

We usually go to the water dispenser to get water. There are two types of people. One kind of person will look at it first Is there any water in the bucket? If so, press the button to collect the water. This is a while loop.

Another kind of person doesn't care. Press the button first. When the water comes out, continue to drink it. If there is no water, go again. Check to see if there is water in the bucket, and then leave silently. This is do...while loop

Syntax

do
{
Code to be executed;
}
while (condition);

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

The for loop and foreach loop will be explained in the next chapter.


Continuing Learning
||
<html> <body> <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br>"; $i++; } ?> </body> </html>
submitReset Code