EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Do While Loop

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Do While Loop

PHP Do While Loop

Introduction to PHP Do While Loop

PHP –Hypertext Pre-processor

A server-side scripting language, PHP is a very popular and widely used open-source language. Initially, PHP was known as –Personal Home Page. In this topic, we are going to learn about PHP Do While Loop.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

PHP Syntax

<?php
//statements to be executed
echo “This is my first php program!”;
?>

Note:
Every statement in PHP ends with a semicolon (;). This technically conveys the PHP engine that this is going to be the end of the statement. And, then the engine moves to the next line and executes the code till semicolon (;).

PHP Loops

In certain situations, we have to use the same block of code many times. In this case, one can make use of loops. Instead of using almost equal code for almost the same condition, by using loops you can run a block of code over and over.

#Following are some of the PHP looping statements.

  • while: a block of code runs as far as the provided condition is ‘True’
  • do…while: a block of code runs at least for once, and repeats the same code as far as the provided condition is ‘True’
  • for: a block of code runs for the provided number of times
  • foreach: a block of code runs for each element in an array

PHP ‘do…while loop’

After having an understanding of ‘while… Loop’, the next step is to understand the logic of ‘do…while loop’. Unless the stated condition is ‘True’ this ‘do…while loop’ can be executed repeatedly.

A small difference between ‘while’ and ‘do…while’ lop is the place where the condition meets its validation point. In ‘while loop’ the condition is tested before executing any statement in the block of code i.e. at the beginning. And, ‘do…while loop’ the condition is tested once, after executing the statements in the block code, then the same processes recur until it is true.

Technically, it can be explained as ‘do…while loop’ is always completed solitary execution, then test suggested condition, and keep on repeating the same block of code while the stated condition stands ‘True’.

Syntax of ‘do…while’

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

Note: ‘do… while loop’ always executes a block of code minimum for a single time while ‘while loop’ does not execute even for a single time. It is because the parameter gets tested after executing the entire block of code.

Flow Chart in PHP

Let’s have a look at the demonstration of one example line by line.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,731 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Example:

  1. <?php
  2. $x=7;
  3. do
  4. {
  5. echo “The expected output is: $x<br>”;
  6. $x++;
  7. }
  8. while($x<=6)
  9. ?>

Output:

The expected output is: 7

Explanation:

  1. This is standard opening tag defined for php language
  2. A value of 7 is allocated to the php variable at the start
  3. ‘do…while loop’ started here
  4. Herewith opening curly braces [{] the php ‘do…while loop’ gets start
  5. Here all the statements inside the ‘do…while loop’ will be executed
  6. php variable value is increased by ‘1’ and the loop continues to execute statements until it turns true.
  7. Herewith closing curly braces [}] the php ‘do…while loop’ ends
  8. The condition is tested here
  9. php closing tag

Hope you understood the details working on the above example.

Now, we will see some more examples for better understanding.

Let’s see a very basic example of printing numbers ‘0 to 9’. With this example, you will be able to write the program for squares of numbers or multiples of number, etc. just by changing the condition.

Example:

<html>
<body>
<?php
$n=0;
do{
echo "$n<br/>";
$n++;
}while($n<=9);
?>
</body>
</html>

Output:

0

1

2

3

4

5

6

7

8

9

Example:

<html>
<body>
<?php
$x0=0;
do {
echo "Executed Statement: $x0 <br />";
echo "this execution is done after the above statement ‘$x0’ is printed <br />";
$x0=$x0+1;
}while ($x0<=5)
?>
</body>
</html>

Output:

Executed Statement: 0

this execution is done after the above statement ‘0’ is printed

Executed Statement: 1

this execution is done after the above statement ‘1’ is printed

Executed Statement: 2

this execution is done after the above statement ‘2’ is printed

Executed Statement: 3

this execution is done after the above statement ‘3’ is printed

Executed Statement: 4

this execution is done after the above statement ‘4’ is printed

Executed Statement: 5

this execution is done after the above statement ‘5’ is printed

Example:

<html>
<body>
<?php
$BookPrice = 15;
do   {
echo "The book price is " . $BookPrice . ". Students can buy this book. <br>";
$BookPrice = $BookPrice + 1;
}
while ($BookPrice <= 10);
echo "The book price is " . $BookPrice . ". Student cannot afford this costly book!";
?>
</body>
</html>

Output:

The book price is 15. Students can buy this book.

The book price is 16. Students cannot afford this costly book!

Now we will see the php program of printing a table of 10.

Example:

<?php
@$tab=$_GET['tab'];
$i=1;
do
{
$t=$tab*$i;
echo $t." ";
$i++;
}
while ($i<=10);
?>
<body>
<form>
Enter Your table<input type="text" name="tab"><br/>
<input type="submit" value="Table">
</form>
</body>

Output:

10 20 30 40 50 60 70 80 90 100

Enter your table

enter your table

Table

Explanation

The above is an example is slightly different. We have made use of one text-box and one button using an HTML script. The main logical part is performed inside the php script.

Very firstly, we have collected the value entered by the user by $_GET.

Variable $i holds value 1.

And, here the logic is applied inside the php code to print the table of 10.

Conclusion

 In the above article, we have come up with essential points on PHP loops and learned about the various types. Specifically, we have learned PHP ‘do…while loop’ in detail. This article gives information about do…while loop, it’s working, and its use with examples. The functioning of the ‘do…while loop’ is very easy to understand.

To summarize, PHP ‘do…while loop’ eliminates the need for executing a similar task again and again. So, If you want to do reduce the workload on PHP language, make use of ‘do…while loop’ frequently.

Recommended Articles

This is a guide to PHP Do While Loop. Here we discuss the information about do…while loop, it’s working, and its use along with examples. You may also look at the following article to learn more –

  1. PHP Frameworks
  2. PHP String Functions
  3. PHP Commands
  4. While Loop in PHP

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More