PHP Loops
Loops in PHP are used to execute the same block of code a specified number of times (iteration). PHP supports the following four loop types.
- for loop
- foreach loop
- while loop
- do...while
PHP for loop
For loops through a block of code a specified number of times. There are basically two types of for loops:
- for
- foreach
for loop
Syntax:
for (initialize; condition; increment)
{
//code to be executed
}
- 'initialize' is used to set the counter’s initial value. It is usually an integer.
- 'condition' is evaluated for each php execution. If it evaluates to true, then execution of the loop continues. If it evaluates to false, the execution is terminated.
- 'increment' is used to increase the initial value of a counter.
Eg:
<?php
for($i=1;$i<=5;$i++)
{
echo $i." x 10 = ".$i*10;
echo '<br>';
}
?>
Output will be:-
1 x 10 = 10
2 x 10 = 20
3 x 10 = 30
4 x 10 = 40
5 x 10 = 50
foreach loop
Syntax:
foreach($array as $array_values)
{
//block of code to be executed
}
- "$array" is the array variable to be looped through.
- "$array_values" is the temporary variable that holds the current array item.
- "block of code to be executed" is for operating on the array values
Eg:
<?php
$fruits = array("apple","orange","banana");
foreach($fruits as $values)
{
echo " Current fruit item : ".$values;
echo '<br>';
}
?>
Output will be:-
Current fruit item : apple
Current fruit item : orange
Current fruit item : banana
Comparing 'for' and 'foreach' loops
for loop | foreach loop |
---|---|
The for loop is used when you know in advance how many times the block of code should be executed. | The foreach loop is used only for iterating arrays and is used to loop through each key/value pair in an array. We need not specify the number of elements in an array. |
for is slower than foreach because it needs some memory to store the counter. | foreach is faster than for loop. |
PHP while loop
It loops through a block of code as long as the specified condition is true.
Syntax:
while(condition)
{
//block of code to be executed
}
- "condition" is the condition to be evaluated by the while loop.
- "block of code to be executed" is the code to be executed if the condition is true.
Eg:
<?php
$i = 1;
while($i <= 5)
{
echo $i." x 10 = ".$i*10;
$i++;
echo '<br>';
}
?>
Output will be:-
1 x 10 = 10
2 x 10 = 20
3 x 10 = 30
4 x 10 = 40
5 x 10 = 50
do while loop
It executes a block of code at least once, and then repeats the loop as long as the 'while' condition is satisfied. Unlike 'for' and 'while' loops, which test the loop condition at the top of the loop, the 'do...while' loop checks its condition at the bottom of the loop.
Syntax:
do
{
//block of code to be executed
} while(condition);
- "do{…}" code executes at least once.
- "condition" is the condition to be evaluated by the while loop.
- "block of code to be executed" is the code to be executed if the condition is true.
Eg:
<?php
$i = 1;
do
{
echo $i." x 10 = ".$i*10;
$i++;
echo '<br>';
} while($i <= 5);
?>
Output will be:-
1 x 10 = 10
2 x 10 = 20
3 x 10 = 30
4 x 10 = 40
5 x 10 = 50
Comparing 'while' and 'do…while' loops
while loop | do…while loop |
---|---|
A while loop evaluates the condition at the beginning of each loop iteration. | It evaluates the condition at the end of the loop iteration. |
If the condition evaluates to false, the loop will never be executed. | The loop will always be executed at least once, even if the condition is false. |