Types of loops in PHP
loops 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. Loops help to decrease code repetition and optimize programs. It allows the programmers to save time and effort by writing less code.
Types of loops
There are mainly 6 loop types in PHP:
- For Loop
- Foreach Loop
- While Loop
- Do-while Loop
- Break statement
- Continue Statement
1. For Loop
The for loop is used when you know how many times you want to execute a statement or a block of statements. For Loop consists of three expressions:
- Initialization: It sets the initial value of the loop variable.
- Condition: It checks if the loop should continue.
- Increment/Decrement: Changes the loop variable after each iteration.
Syntax:
for ( Initialization; Condition; Increment/Decrement ) {
// Code to be executed
}
Example:
<?php
// Code to illustrate for loop
for ($i = 1; $i <= 5; $i++ ) {
echo $i . "\n";
}
?>
Output:
1 2 3 4 5
2. Foreach Loop
The foreach loop is a block of code for each element in an array or each property in an object. For every counter of loop, an array element is assigned, and the next counter is shifted to the next element. The foreach loop simplifies working with arrays and objects by automatically iterating through each element.
Syntax:
<?php
foreach (range(start, end) as $value) {
// code using $value
}
?>
or
foreach ($array as $value) {
// use $value
}
Example:
<?php
foreach (range(1, 5) as $i) {
echo $i . "\n";
}
?>
Output:
1 2 3 4 5
3. While Loop
The while statement loop is a block of code as long as the condition specified in the while statement evaluate to true. The condition is checked at the beginning of each iteration, which means that if the condition is initially false, the code block will not run even once.
Syntax:
while(condition){
// Code to be executed
}
Example:
<?php
// Code to illustrate while loop
$i=1;
while ($i <= 5) {
echo $i . "\n";
$i++;
}
?>
Output:
1 2 3 4 5
The while loop is also an entry control loop like for loops. It first checks the condition at the start of the loop, and if it’s true then it enters into the loop and executes the block of statements and goes on executing it as long as the condition holds true.
4. Do-while Loop
The do-while loop is a variant of while loop, which evaluates the condition at the end of each loop iteration. It will execute a block of code at least once, then it’ll repeat the loop as long as a condition is true.
Syntax:
do {
// code to be executed
} while (condition);
Example:
<?php
// Code to illustrate do-while loop
$i = 1;
do {
echo $i . "\n";
$i++;
} while ($i <= 5);
?>
Output:
1 2 3 4 5
5. Break Statement
In PHP, the break statement is used to immediately break out of different kind of loops. The break statement inside the looping block takes the program flow outside the block, abandoning the rest of iterations that may be remaining.
The break statement is normally used in conditionally, otherwise, the loop will terminate without completing the first iteration itself.
Syntax:
while(expr){
if (condition){
break;
}
}
Example:
<?php
// Code to illustrate break statement
$i = 1;
while ($i<=10){
echo $i . "\n";
if ($i>=5){
break;
}
$i++;
}
?>
Output:
1 2 3 4 5
6. Continue Statement
Continue is a “loop control statement” in PHP. The continue statement is used to skip the current iteration of a loop, and continue with the next iteration.
Syntax:
for (initialization; condition; increment) {
// code
if (some_condition) {
continue; // skips to the next iteration
}
// code that will be skipped if continue runs
}
Example:
<?php
for ($i = 0; $i <= 5; $i++ ) {
if ($i == 0) {
continue; // Skip the rest of this iteration when $i is 0
}
echo $i . "\n";
}
?>
Output:
1 2 3 4 5