PHP While Loop

Loop through a code block 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:

1. while - As long as the specified condition is true, the code block will be executed in a loop

2. do...while - First execute the code block once, and then repeat the loop when the specified condition is true

3. for - Loop through the code block a specified number of times

4. foreach - Based on each element in the array To loop through a block of code

while loop

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

while is a Boolean loop. If the value of while (Boolean judgment) is true, the code enclosed in curly brackets will be executed. If it is false, stop and execute the subsequent code.

Syntax

while (condition)
{
Code to be executed;
}

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.. The .while statement executes the code at least once, then checks the condition and repeats the loop as long as the condition is true.

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 difference between do...while and while

The difference between do...while and while is that their values ​​are checked at different times.

do-while Regardless of whether the while judgment is true or not, the code block loop statement is executed once, and it is guaranteed to be executed once (the truth value of the expression is checked after each loop ends). However, our previous while loop will check the Boolean judgment area and execute it if it is true. If it is not established, it will not be executed.

Let’s use the code to verify it:

<?php 
$i = 0; 
do 
{ 
echo $i; 
} 
while ($i > 0); 
?>

In the above code, $i is definitely not greater than 0, and it is also executed.

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