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++;
  } 
?>
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
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

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.

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 18/01/2021
3176 People Used
Only 97 Left
Rating
5.0