Loops in PHP
In PHP loops are used to create the identical chunk of code in a particular number of times, rather to add a certain code-lines in a script.
In PHP we have the four looping statement
- while
- do....while
- for
- foreach
while loop
while loop creates the block of code again and again until the particular condition is true .The value declaration is checked from the beginning of the loop and it stops when they meet the final condition which is true, If at the begining of the code the condition is false the nested condition won't run.
Syntax :-
<?php
while (condition) {
code to be executed;
}
?>
Example :-
<?php
$x = 10;
while ($x < 17)
{
echo $x + 2 . "<br/>";
$x++;
}
?>
Output
12
13
14
15
16
17
18
Example :-
<?php
$x = 10;
$number = 30;
while ($x < 12)
{
$number--;
$x++;
}
echo ("Loop stop at x = $x and number = $number");
?>
Output
Loop stop at x = 12 and number = 28
do....while loop
The do... while loop is very similar to the while loop except it check the redundancy of code from the end rather than from the beginning and repeat the loop while the particular condition is true.
Syntax :-
<?php
do {
code to be executed;
} while (condition);
?>
Example :-
<?php
$i = 20;
do
{
echo "The number is: $i <br/>";
$i++;
}
while ($i <= 27);
?>
Output
The number is: 20
The number is: 21
The number is: 22
The number is: 23
The number is: 24
The number is: 25
The number is: 26
The number is: 27
Example :-
<?php
$x = 10;
$number = 30;
do
{
$x++;
}
while ($x < 35);
echo ("Loop stop at $x");
?>
Output
Loop stop at 35
for loop
The for loop is a little more complex then another loop the code is executed with a particular number of times.The for loop contains some of the parameters to fulfill the condition of the code.
Some of the parameters of for loop are:-
init counter: load the loop to reverse the value
test counter: Calculate for each loop redundancy. If the condition is TRUE, the loop continues. If it's condition is FALSE, the loop ends.
increment counter: It Increases the loop reverse value
Syntax :-
<?php
for (init counter; test counter; increment counter) {
code to be executed;
}
?>
Example :-
<?php
for ($i = 3; $i <= 10; $i++) {
echo "The number is: $i <br>";
}
?>
Output
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
Example :-
<?php
$x = 20;
$y = 40;
for ($i = 0; $i < 10; $i++)
{
$x+= 8;
$y+= 4;
}
echo ("The x = $x and y = $y");
?>
Output
The x = 100 and y = 80
foreach loop
The foreach loop works on array and object.It will raise the issue when you use variable with different data type or an uninitalized variable. It is used to loop each key in an array.
Syntax :-
<?php
foreach ($array as $value) {
code to be executed;
}
?>
Example :-
<?php
$Car = array("Audi", "Bentley", "BMW", "Bugatti", "Buick");
foreach ($Car as $name) {
echo "$name <br>";
}
?>
// remove cide
Output
Audi
Bentley
BMW
Bugatti
Buick
Example :-
<?php
$number = array(array( 363, 332, 344, 564, 555);
foreach($number as $value)
{
echo "The number is $value <br/>";
}
?>
Output
The number is 363
The number is 332
The number is 344
The number is 564
The number is 555