🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

Code has been added to clipboard!

PHP while Loop: Correct Usage of while and do while Explained

Reading time 3 min
Published Aug 8, 2017
Updated Sep 24, 2019

PHP while loop executes a block of code multiple times. This function also exists in other programming languages, such as Java and C++.

PHP loops work in this manner: they repeat the same action over and over again until a specific effect is reached. PHP loops come in four types, each represented by a separate statement: while, do...while, for and foreach.

In this tutorial, we focus on the first two types of PHP loops. If you wish to learn more about for and foreach, you are always welcome in our next lesson.

PHP while Loop: Main Tips

  • In order not to write the same line of code multiple times, use loops.
  • PHP while and do...while loops run a block of code multiple times until the specified condition is no longer true.
  • while loop PHP checks the condition before running the block of code, while do...while does that after running the code.

while Loop Explained

For a PHP while loop to be executed correctly, you must follow the correct syntax:

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

Let's review the example we have below. Our code sets the value of the variable $xyz to 1. Then, it checks if $xyz is less than 5, and continues performing PHP while loop (running the code block repeatedly).

The variable value will increase by one with every loop, and the code will display the updated value every time:

Example
<?php 
  $xyz = 1;
  while($x < 5)
  {     
      echo "Current value: $xyz <br>";
      $xyz++;
  } 
?>
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

Syntax of do while Loop

The correct syntax of PHP do...while loop looks like this:

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

In the code example below, you can see that the variable $xyz is assigned a value of 1. Then, the script prints out the variable value and increases it by 1. After that, it checks if the value is less than 5. If it is indeed true, PHP do while loop continues:

Example
<?php
  $xyz = 1;
  do 
  {    
     echo "Current value: $xyz <br>";
     $xyz++;
   } while ($xyz < 5);
?>

Note: in the do while example, code always runs the first time despite of the condition being true or not.

PHP while Loop: Summary

  • Usage of loops eliminates the need to write the same block of code multiple times to make it run more than once.
  • To execute a piece of code multiple times until a condition becomes false, use while and do...while loops.
  • while checks whether the condition is true before it loops through the code. do...while checks this only after running.