How to use Do While Loops in PHP - Coding Cyber

How to use Do While Loops in PHP

How to use Do While Loops in PHP

Other Articles in PHP Loops Section

Quickly Navigate to Other Sections in PHP Tutorials

PHP Basics
PHP Data Types
PHP Advanced Data Types
PHP Logical Statements
PHP Operators
PHP Loops
User Defined Functions in PHP
Useful PHP Functions

do while() Loop in PHP, do while loops are same as while loops except executes the code once. Then checks the condition & repeats the loop.

Syntax of do while() Loop in PHP

do {
code to be executed;
} while (condition is true);

Example code of do while() Loop in PHP
Here in this example code, we are assigning variable x as 1. Then do while loop will executes the statement after that increments variable x and then checks the condition.

x = 1, displays 1, then increments to 2, still 2 is less than 5
Now x = 2, displays 2, then increments to 3, still 3 is less than 5
Now x = 3, displays 3, then increments to 4, still 4 is less than 5
Now x = 4, displays 4, then increments to 5, still 5 is equal to 5
Now x = 5, displays 5, then increments to 6, but now 6 is less than or equal to 5. do while loops exits here
$x = 1;

do {
echo “The number is: $x
“;
$x++;
} while ($x <= 5);

Related Posts
No related posts for this content
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: