PHP While Loop

PHP While, Do-While Loop

In this tutorial, we will learn about PHP while loop and do while Loop in PHP but firstly we have to know about Loops. Loops are used to execute the same block of code again and again, as long as a certain condition is true.

In PHP, we have the following types of loops :

while loopΒ – loops through a block of code as long as the specified condition is true.

do whileΒ loop– loops through a block of code once, and then repeats the loop as long as the specified condition is true.

for loopΒ – loops through a block of code a specified number of times.

foreachΒ loop – loops through a block of code for each element in an array.

PHP While loop

PHP while loops can be used to traverse a set of code like for loop. The while loop in PHP executes a block of code repeatedly until the condition is FALSE. Once the condition gets FALSE, it exits from the body of the loop.

It should be used if the number of iterations is not known.

The while loop is also called an Entry control loop because the condition is checked before entering the loop body. This means that first the condition is checked. If the condition is true, the block of code will be executed.

Syntax :

while(condition){
//code
}

β€œwhile(…){…}” is the while loop block code

β€œcondition” is the condition to be evaluated by the while loop

β€œcode” is the code to be executed if the condition gets satisfied

Example :

<?php    
$n=1;    
while($n<=10){    
echo "$n<br/>";    
$n++;    
}
?>

Output :

1
2
3
4
5
6
7
8
9
10

PHP do while loop

The difference between a while and do while loop in PHP. do while loop is executed at least once before the condition is evaluated.

Let’s now look at the basic syntax of a do while loop.

Syntax

do {
  code;
} while (condition is true);

β€œdo{…} while(…)” is the do… while loop block code

β€œcondition” is the condition to be evaluated by the while loop

β€œcode” is the code that is executed at least once by the do… while loop

Example :

The example below first sets a variable $x to 1 ($x = 1). Then, the do-while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5:

<?php
$x = 1;

do {
  echo "The number is: $x <br>";
  $x++;
} while ($x <= 5);
?>

Output :

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

PHP While Loop We have already discussed the PHP For Loop. Now we are discussing another loop.

In PHP, we have the following looping statements:

PHP While Loop

The while loop executes a block of code as long as the specified condition is true.

Syntax :

while (condition) {
    block of code;
}

Example :

<?php
$i = 0;
while ($i < 5)
{
	echo "<br> value of i is " . $i;
	$i++;
}
?>

Output :

value of i is 0
value of i is 1
value of i is 2
value of i is 3
value of i is 4

At the end of each execution, the condition is checked again. If the condition has a true value, the block of code will be executed again. If the condition has a false value, the loop was broken.

Do While loop

The do while is the same as the while loop but the main difference is the block of code will be executed at least once before the condition is checked for a true or false value then repeats the loop as long as the specified condition is true.

Syntax :

do {
    block of code;
} while (condition);

Example :

<?php
$i = 0;
do
{
 echo β€œ<br> value of i is” . $i;
 $i++;
} while ($i < 5);
?>

Output :

value of i is 0
value of i is 1
value of i is 2
value of i is 3
value of i is 4

Infinite While loop in PHP

It is an infinite loop which will execute the block of code till a break statement is applied explicitly. Therefore while(1), while(true), or while(-130) Β all will give an infinite loop only.

The use of Infinite while loop is in the Client-Server program. In the program, the server runs in an infinite while loop to receive the packets sent from the clients.

Syntax :

while (1) or while (true) or while (any non-zero integer) {
    block of code;
}

Example :

<?php
while (1)
{
	echo "<br> developer helps";
}
?>

Output :

developer helps
developer helps
developer helps
developer helps
developer helps
developer helps
developer helps
developer helps...... infinity times

While (0) or While (false) loop

The condition will always be false in this case so a block of code will never get executed.

Syntax :

while (0) or while (false){
    block of code;
}

Example :

<?php
while (0)
{
	echo "<br> developer helps";
}
echo "while loop not execute";
?>

Output :

while loop not execute

More Related Post

Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.

Leave a comment

Your email address will not be published. Required fields are marked *