PHP While Loop


In the last tutorial, we have discussed for a loop. There, we also discussed loops in PHP . On this, we will talk about while loop. Loop is an interactive control structure, which executes the number block of code until the specified number. In the simple words, the loops base on condition until the condition matches, then the code is executed and when the condition does not match then the loop does not work.

Definition and use of the While Loop

While loop depends on the expression if a test expression is true, while statement will execute the block of code. After the code executing the while loop checks the expression again and while loop will continue until the condition (expression ) is false.

In simple words, if the condition is true then execute the code of block and after code executed while statement checks the condition is true or not if the condition is true then the loop will continue and the condition is not true then exit the block of code.

In other words, in while loop the compiler checks the condition first and executes the code. After that, increment for the next loop. If the increment value matches the condition, then the compiler executes the code of block again. The compiler continues to execute the code of block until the condition matches. If the condition does not match then the compiler gets exit from the code of the block. That is known while loop.

Syntax

In the given syntax, you will get better understanding about while loop.

while (condition) 
{ 
code to be executed; 
}

In this syntax, we give an expression with while loop inside the parenthesis. The expression is also called condition. The condition of this while loop gives to the compiler. How many numbers does the compiler have to execute the block of code.

Examples of While Loop

The following example ,We create a table with the help of the while loop.

Make a table

<?php 
$i=1;
//first check the condition 
while($i<=15)
{
	//after check the condition code will execute .
	echo $i."
"; $i++; } ?>
Run

In this above example, we have created a variable. After Creating Variables, we create a condition inside the while loop, the loop continues, when the condition is true when the condition is false then the loop does not continue and the compiler gets exit from the code of the block.

Pyramid pattern using While Loop

With the help of while Loop, we can also create pyramid patterns.

Example

<?php 
$x=1;
while($x<=5)
{
    $y=1;
    while($y<=$x)
    {
      echo"*  ";
      $y++;      
    }
    echo"
"; $x++; } ?>

Output

*  
*  *  
*  *  *  
*  *  *  *  
*  *  *  *  *  

In the above example, we have created two variables and used while loop inside the while loop to create a Pyramid pattern.

Year of birth using While Loop

Any registration form is required to create the year of birth. Year of birth can be easily displayed with the help of While Loop.

Year of Birth

<?php 
$i=2020; 
while($i>1989) 
	
	{
		  $i--;
	echo $i."
"; } ?>

Output

  
2019
2018
2017
2016
2015
2014
2013
2012
2011
2010
2009
2008
2007
2006
2005
2004
2003
2002
2001
2000
1999
1998
1997
1996
1995
1994
1993
1992
1991
1990
1989

In the above example, we have displayed the year of birth. With the help of a while loop, you can display the year of birth from the initial year to ending the year. In this example, we have used the decrement operator (--) . The first value decrement and then print. With the while loop, you can easily execute the block of code until the condition is true. When the condition is false, the compiler gets exit from the block.


Please Share

Recommended Posts:-


Live Chat

Hello! How can we assist you today?