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++;
  } 
?>

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.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()