Loading...

PHP While Loop, Do while loop and How to Use Them in PHP. (2024)

Uvision Blogs Details

PHP While Loop, Do while loop and How to Use Them in PHP.

What is PHP Loops and How to Use them.

PHP Loops are used to implement the same block of code again and again multiple times until and unless a specific condition is happened. The basic idea behind a PHP loops is to help the users to save both time and effort of writing the same code multiple times.

 PHP supports different kinds of loops.

But In this tutorial we will learn about PHP while and do while loops which are also used to implement looping in PHP programing.

PHP while Loop.

PHP while statement is used to execute a block of code continually until the set condition fulfilled. You can use a while loop to read records returned from a database query.

Syntax of PHP while Loop.

<?php
while (condition){
block of code to be executed;
}
?>

Condition: is the condition to be evaluated by the while loop.

Block of code: is the code to be executed if the condition gets satisfied.

Below is the flow chart to understand that how PHP while loop Works?

php while loop chart flow

Now we understand this PHP while loop with example. Let’s try to print numbers from 1 to 5.

<?php  
$a = 1;
 
while($a <= 5) {
  echo "The number is: $a <br>";
  $a++;
} 
?>  
OutPut Of the above code.
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

Explanation of the above code.

$a = 1; - Initialize the loop counter ($a), and set the start value to 1

$a <= 5 - Continue the loop as long as $a is less than or equal to 5

$a++; - Increase the loop counter value by 1 for each iteration

PHP do while Loop.

The do-while loop is alternate of while loop, which evaluates the condition at the end of each loop iteration. If the condition is true, the statement is repeated as long as the specified condition evaluated to be true.

Syntax of PHP do while Loop.

do {

  code to be executed;

} while (condition is true);

 Block of code: is the code that is executed at least once by the do… while loop.

Condition: is the condition to be evaluated by the do while loop.

Below is the flow chart to understand that how PHP do while loop Works?

php do while loop chart flow

Now we understand this PHP do while loop with example. Let’s try to print numbers from 1 to 10.

<?php
$i = 1;
do{
    
    echo "The number is " . $i . "<br>";
	$i++;
}
while($i <= 10);
?>
Output Of the above code.
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 10

Difference between PHP While and do while Loops.

While Loop.

In a while loop, the condition is tested at the start of each loop iteration, so if the condition evaluates to false, the loop will never be executed.

Do while Loop.

In a do-while loop the loop will always be executed once, even if the conditional expression is false because the condition is evaluated at the end of the loop iteration rather than the beginning.

In Short.

  • The while loop is used to execute a block of code as long as the set condition is made to be false
  • The do while loop is used to execute the block of code at least once then the rest of the execution is dependent on the evaluation of the set condition

Other Related Posts

How to use PHP for Loop and foreach Loop

We hope you may learn how to use PHP while loop and do while loop. Please share your thoughts and queries in the contact section.