PHP while Loop and do while Statement Break Continue Code Example

PHP while Loop Statement

PHP while Loop is used to execute the part of code multiple times. This Loop is also working similarly to the for loop but has a little bit change in the syntax.

All the three statements are broke down into 3 part. Firstly we initialize the variable than give the condition and then increment/decrement statement.

while Loop Syntax

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		initialization;
		
		while(condition)
		{
			code to be executed;
			increment/decrement;
		}
	?>
  </body>
</html>

Example 1 – while Loop Example code to print series of numbers

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$x = 1;
		while($x<=5)
		{
			echo $x . "<br />";
			$x++;
		}
		
	?>
  </body>
</html>

In the above code, we describe how you can use while Loop and here we print the counting from 1 to 5.

PHP while Loop Example Code Syntax
Fig.1 – while Loop Syntax

Print the Table of 2 using while Loop

The given below code is used to print the table of two with while Loop

Example 2 – Print the table of 2 using while Loop Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$x = 1;
		while($x<=10)
		{
			echo "2 X " . $x . " = " . $x * 2 . "<br />";
			$x++;
		}
		
	?>
  </body>
</html>

In the above code, we describe how you can use while Loop and here we print the table of 2.

while Loop Code to print number 2 table
Fig.2 – while Loop example code to print table of 2.

PHP while Loop Break and Continue Statement Syntax and Example

Break and Continue both is used to for the different purpose such Break is used to stop the loop and where Continue is used to skip the statement.

Example 3 – while Loop Break Syntax Example to exit from the loop in certain condition

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$x = 1;
		while($x<=10)
		{
			echo "2 X " . $x . " = " . $x * 2 . "<br />";
			if($x==5)
			{
				break;				
			}
			$x++;
		}
	?>
  </body>
</html>

In the above code, we describe how you can use while Loop and here we print the table of 2 but we break the loop when the value of variable $x will be 5 then the execution we will not do and the execution will end.

PHP while Loop Break Statement example
Fig.3 – Break statement example to exit from the while loop.

Example 4 – while Loop Continue Example Code to skip certain condition

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$x = 0;
		while($x<10)
		{
			$x++;
			if($x==7)
			{
				continue;				
			}
			echo "2 X " . $x . " = " . $x * 2 . "<br />";
		}
		
	?>
  </body>
</html>

In the above code, we describe how you can use while Loop and here we print the table of 2 but we Continue the loop when the value of variable $x will be 7 then the execution will be skipped once.

PHP while Loop Continue example
Fig.4 – while Loop Continue example to skip execution of some statements.

Note: You can use break and continue in do while loop also.

PHP do while Loop

PHP do while loop is also used to execute a part of the code for specified times. This loop is also similar to for and while but have different syntax and working procedure.

do….while Loop Syntax

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		initialization;
		do
		{
			code to be executed;
			increment/decrement;
		}while(condition)
	?>
  </body>
</html>

Example 5 – do….while Loop Example code to print series of numbers

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$x = 0;
		do
		{
			$x++;
			echo  $x . "<br />";
		}while($x<=5)
		
	?>
  </body>
</html>

In the above code, we describe how you can use do while Loop and here we print the counting from 1 to 5. the loop will be stopped when the value of $x will less than or equal to 5.

PHP do while Loop Syntax
Fig.5 – do while Loop example to print number series.

Difference between while & do…while Loop

The major difference between the while loop and do….while loop is that the while loop checks the condition first and then execute the code where in do…while loop firstly executes the code then check the condition. It means that the do…while loop executes at least once even if the condition is false.

Example 6 – Demonstrate the difference between them

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		
		$x = 7;
		do
		{
			$x++;
			echo  $x . "<br />";
		}while($x<=5)
		
	?>
  </body>
</html>

In the above code, we can use do while Loop and the value of the variable $x is 7 but the condition is not valid to that variable. But the execution of the loop will start and execute the code once. In the above code, we can use do while Loop and the value of the variable $x is 7 but the condition is not valid to that variable. But the execution of the loop will start and execute the code once.

do while example
Fig.6 – do while Loop example output screenshot.
You can leave a response, or trackback from your own site.
Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape