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.
Contents
PHP while Loop: Main Tips
- In order not to write the same line of code multiple times, use loops.
- PHP
while
anddo...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, whiledo...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:
<?php
$xyz = 1;
while($x < 5)
{Â Â Â
echo "Current value: $xyz <br>";
   $xyz++;
}
?>
- 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
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- 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:
<?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
anddo...while
loops. while
checks whether the condition is true before it loops through the code.do...while
checks this only after running.