What is loops statement in PHP?
When you write code, you often want to execute the same block of code a certain number of times.
So, we can use loops instead of adding several almost identical lines of code to a script.
With a loop, you can keep executing the same block of code as long as a certain condition is true.
How many types of loops are there in PHP?
PHP supports four different types of loops:
- for loop – Iterates through a block of code until a specified counter value is reached.
- foreach loop – Iterates through a block of code for each array element.
- while loop – Iterates through a block of code as long as the specified condition evaluates to true.
- do…while loop – Once the block is executed, the condition is then evaluated. If the condition is met, the statement is repeated until the condition is no longer met.
1. for loop statement
When you know how many times you want to run a statement or a block of statements, you use the for
statement.
A for loop is a type of loop used when the user knows ahead of time how many times the block needs to be executed. That is, the number of times the process is repeated is already known.
These loops can also be called “entry-controlled loops.” The code has three main parts: the initialization, the test condition, and the increment.
Syntax:
<?php
for (initialize; condition; increment){
//code to be executed
}
?>
In for loops, the loop is controlled by a loop variable. First, give this loop variable a value, and then check to see if that value is less than or more than the value of the counter.
If the statement is true, the loop body is run and the variable in the loop is changed. Each step is done again and again until the exit condition is met.
- Initialization Expression: In this expression, we need to set some value as the initial value for the loop counter. for example: $num = 1;
- Test Expression: We have to test the condition in this expression. If the condition is true, the body of the loop will be run, and then we will go to the update expression. If the condition is false, we will leave the for loop. For example: $num <= 10;
- Update Expression: After the loop body is run, this expression adds or subtracts some value from the loop variable. for example: $num += 2;
Example:
The example below loops five times and changes the values of two variables each time through the loop.
<html> <body> <?php $a = 0; $b = 0; for( $i = 0; $i<5; $i++ ) { $a += 10; $b += 5; echo "\nThe value of a is $a & the value of b is $b"; } echo ("\n\nAt the end of the loop a = $a and b = $b" ); ?> </body> </html>
Output:
The value of a is 10 & the value of b is 5
The value of a is 20 & the value of b is 10
The value of a is 30 & the value of b is 15
The value of a is 40 & the value of b is 20
The value of a is 50 & the value of b is 25
At the end of the loop a = 50 and b = 25
2. foreach loop statement
Arrays can be looped through with the foreach
statement. At each pass, the value of the current array element is put into $value
and the array pointer is moved by one.
The next pass will then process the next array element.
Syntax:
<?php
foreach (array as value) {
//code to be executed;
}
?>
Example:
Here’s an example of how to list the items in an array.
<html>
<body>
<?php
$array = array( 'a', 'b', 'c', 'd', 'e');
foreach( $array as $value ) {
echo "Value is $value\n";
}
?>
</body>
</html>
Output:
Value is a
Value is b
Value is c
Value is d
Value is e
3. while loop statement
While a test expression is true, the while
statement will execute a block of code.
The code block will be performed if the test expression returns a true value. After the code has been executed, the test expression is re-evaluated, and the loop continues until the test expression evaluates to false.
Additional information from PHP Documentation, the meaning of a
statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while
expression evaluates to while
true
.
Syntax:
<?php
while (condition){
//block of code to be executed;
}
?>
Example:
This example decrements the value of a variable with each iteration of the for loop, while the counter increases until it reaches 10 when the evaluation is false and the for loop terminates.
<html>
<body>
<?php
$i = 0;
$num = 50;
while( $i < 10) {
$num--;
$i++;
echo "The value of num is $num and the value of i is $i\n";
}
echo ("\nThe loop stopped" );
?>
</body>
</html>
Output:
The value of num is 49 and the value of i is 1 The value of num is 48 and the value of i is 2 The value of num is 47 and the value of i is 3 The value of num is 46 and the value of i is 4 The value of num is 45 and the value of i is 5 The value of num is 44 and the value of i is 6 The value of num is 43 and the value of i is 7 The value of num is 42 and the value of i is 8 The value of num is 41 and the value of i is 9 The value of num is 40 and the value of i is 10 The loop stopped
4. do…while loop statement
A block of code is run at least once by the do…while
statement and the loop are continued as long as the condition remains true.
Syntax:
<?php
do{
//block of code to be executed
}
?>
Example:
In this example, the value of the variable I will be increased at least once, and it will continue to be increased as long as its value is less than 10.
<html>
<body>
<?php
$i = 0;
$num = 0;
do {
$i++;
echo "The value of i is $i\n";
}
while( $i < 10 );
echo ("\nThe loop stopped at i = $i" );
?>
</body>
</html>
Output:
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
The value of i is 5
The value of i is 6
The value of i is 7
The value of i is 8
The value of i is 9
The value of i is 10
The loop stopped at i = 10
Break statement
With the PHP break
keyword, you can stop a loop from running before it is done.
Inside the statement, block is where the break statement goes. It gives you full control, and you can leave the loop whenever you want.
As soon as a loop ends, the next statement in the loop will be run.
Syntax:
<?php
while (expr)
{
..
..
if (expr1)
break;
..
..
}
?>
Example:
In the following example, when the counter reaches 3, the condition test becomes true, and the loop ends.
<html>
<body>
<?php
$i = 0;
while( $i < 10) {
$i++;
echo "Value of i is $i\n";
if( $i == 3 )break;
}
echo ("Loop stopped at i = $i" );
?>
</body>
</html>
Output:
Value of i is 1
Value of i is 2
Value of i is 3
Loop stopped at i = 3
continue statement
The PHP continue
keyword stops the current loop iteration, but it does not end the loop.
Like the break
statement, the continue
statement is in the statement block that contains the code that the loop runs. It is preceded by a conditional test, just like the break
statement.
When a pass comes across a continue
statement, the rest of the loop code is skipped and the next pass begins.
Syntax:
<?php foreach(expr) { .. .. if (expr1) continue; .. .. } ?>
Example:
In the example below, the loop prints the value of the array, but if a certain condition is met, it skips the code and moves on to the next value.
<html>
<body>
<?php
$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value ) {
if( $value == 3 )continue;
echo "Value is $value \n";
}
?>
</body>
</html>
Output:
Value is 1
Value is 2
Value is 4
Value is 5
Related Articles
- PHP Array_Keys With Example Programs
- PHP Capitalize First Letter with Best Example
- Decision Making in PHP (With Advanced Examples)
- PHP Arrays (With Advanced Program Examples)
- Array Push PHP (With Detailed Explanation)
Summary
In summary, we have discussed the different types of loops in PHP and also provided some flowcharts for better understanding.
We also talked about the break and continue statements, which are the control statements of PHP loops.
Lastly, if you want to learn more about Types of Loops in PHP, please leave a comment below. We’ll be happy to hear it!