In this article we are going to cover Loop in PHP with Examples, while loop in PHP, do-while loop in PHP, foreach loop in PHP, Break and Continue statement in PHP, Difference between while and do-while loop in PHP
What is Loop in PHP?
In PHP, loops are used to execute the same block of code again and again, as long as a certain condition is true.
PHP supports following loops:
- for loop in PHP
- while loop in PHP
- do-while loop in PHP
- foreach loop in PHP
#1. for loop in PHP with Examples :
In PHP, loops through a block of code a specified number of times. This for loop is used when you already know how many times you want to execute a block of code.
This type of loops are also known as entry-controlled loops. There are three main parameters to the code, namely the initialization, the test condition and the counter.
Syntax:
for (initialization expression; test condition; increment/decrement expression) {
// code to be executed
}
Initialization Expression : Initialize the loop counter value. The initial value of the for loop is done only once. for example: $no = 1;
Test Expression: Evaluate each iteration value. The loop continuously executes until the condition is false. If true, the loop execution continues, otherwise the execution ends. For example: $no <= 15;
Increment/Decrement: It increments or decrements the value of the variable.
Example of for loop in PHP:
<!DOCTYPE html>
<html>
<body>
<?php
for ($no = 0; $no <= 10; $no++) {
echo "The number is: $no <br>";
}
?>
</body>
</html>
Output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10

#2. while loop in PHP with :
In PHP, loops through a block of code as long as the specified condition is true.
The while loop executes a block of code repeatedly until the condition is false. Once the condition gets false, it exists from the body of loop.
Syntax :
While (if the condition is true) {
// code is executed
}
Example of while loop in PHP :
<!DOCTYPE html>
<html>
<body>
<?php
$no = 1;
while($no <= 5) {
echo "The number is: $no <br>";
$no++;
}
?>
</body>
</html>
Output:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

#3. do-while loop in PHP with Examples:
In PHP, loops through a block of code once, and then repeats the loop as long as the specified condition is true. A statement is executed at least once on using the do…while loop. After executing once, the program is executed as long as the condition holds true.
Syntax:
do {
//code to be executed;
} while (condition is true);
Example of do-while loop in PHP:
<!DOCTYPE html>
<html>
<body>
<?php
$no = 5;
do {
echo "Welcome to FOSSTechNix! </br>";
$no++;
} while ($no < 10);
?>
</body>
</html>
Output:
Welcome to FOSSTechNix!
Welcome to FOSSTechNix!
Welcome to FOSSTechNix!
Welcome to FOSSTechNix!
Welcome to FOSSTechNix!

Difference between while and do-while loop in PHP
while Loop | do-while loop |
The while loop is also named as entry control loop. | The do-while loop is also named as exit control loop. |
Condition checks first, and then block of statement executes. | Block of statement executes first and then condition check. |
The body of the loop does not execute if the condition is false. | The body of the loop execute at least once, even if the condition is false. |
The loop does not use a semicolon to terminate the loop. | Do-while loop use semicolon to terminate the loop. |
#4. foreach loop in PHP with Examples:
In PHP, loops through a block of code for each element in an array. It works only on array and object. It will issue an error if you try to use it with the variables of different datatype.
In foreach loop, we do not need to increment the value.
Syntax:
foreach ($array as $value) {
//code to be executed
}
Example of foreach loop in PHP:
<!DOCTYPE html>
<html>
<body>
<?php
$fruits = array("apple", "banana", "cherry", "orange");
foreach ($fruits as $value) {
echo "$value <br>";
}
?>
</body>
</html>
Output:
apple
banana
cherry
orange

Break and Continue statement in PHP :
#1. Break statement in PHP with Examples:
The break statement can also be used to jump out of a loop.
Example of Break statement in PHP:
<!DOCTYPE html>
<html>
<body>
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 5) {
break;
}
echo "The number is: $x <br>";
}
?>
</body>
</html>
Output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4

#2. Continue statement in PHP:
In PHP, the continue statement breaks one iteration, if a specified condition occurs, and continues with the next iteration in the loop.
Example of continue statement in PHP :
<!DOCTYPE html>
<html>
<body>
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 5) {
continue;
}
echo "The number is: $x <br>";
}
?>
</body>
</html>
Output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 6
The number is: 7
The number is: 8
The number is: 9

The star triangle made using for loop in PHP :
<!DOCTYPE html>
<html>
<body>
<?php
for($i=0;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
?>
</body>
</html>
Output:
*
* *
* * *
* * * *
* * * * *

Conclusion:
We have covered Loop in PHP with Examples, while loop in PHP, do-while loop in PHP, foreach loop in PHP, Break and Continue statement in PHP, Difference between while and do-while loop in PHP
Related Articles:
- Introduction about PHP [PHP 7]
- Date and Time in PHP with Examples [2 Steps]
- 3 Data Types in PHP with Examples
- Form Validation in PHP with Examples
- 3 Types of Arrays in PHP with Examples
Reference: