Loops
Loops
LOOPS
• Loops are used to execute the same block of code again and again, as
long as a certain condition is met.
• The basic idea behind a loop is to automate the repetitive tasks within
a program to save the time and effort.
<?php
$i = 1;
while($i <= 3){
$i++;
echo "The number is " . $i . "<br>";
}
?>
OUTPUT:
The number is 2
The number is 3
The number is 4
PHP do…while Loop
• The do-while loop is a variant of while loop, which
evaluates the condition at the end of each loop iteration.
OUTPUT:
The number is 2
The number is 3
The number is 4
Difference between while and do-while loop
The while loop is also named The do-while loop is also named
as entry control loop. as exit control loop.
The body of the loop does not The body of the loop executes at
execute if the condition is false. least once, even if the condition is
false.
Condition checks first, and then Block of statements executes first
block of statements executes. and then condition checks.
This loop does not use a Do-while loop use semicolon to
semicolon to terminate the loop. terminate the loop.
PHP for Loop
• The for loop repeats a block of code as long as a certain
condition is met. It is typically used to execute a block of
code for certain number of times
• The parameters of for loop have following meanings:
- initialization — it is used to initialize the counter
variables, and evaluated once unconditionally before the
first execution of the body of the loop.
- condition — in the beginning of each iteration, condition
is evaluated. If it evaluates to true, the loop continues and
the nested statements are executed. If it evaluates to false,
the execution of the loop ends.
- increment — it updates the loop counter with a new
value. It is evaluate at the end of each iteration.
PHP for Loop(contd.)
<?php
for($i=1; $i<=3; $i++){
echo "The number is " . $i . "<br>";
}
?>
OUTPUT:
The number is 1
The number is 2
The number is 3
PHP foreach Loop
• The foreach loop is used to iterate over arrays.
• It is used to loop through each key/value pair in an array.
<?php
$colors = array("Red", "Green", "Blue");
OUTPUT:
Red
Green
Blue
PHP foreach Loop(contd.)
<?php
$superhero = array(
"name" => "Peter Parker",
"email" => "[email protected]",
"age" => 18
);
OUTPUT:
name : Peter Parker
email : [email protected]
age : 18
PHP Break
• The break statement can also be used to jump out of a
loop.
OUTPUT:
1
2
3
4
5
PHP Break: inside loop(contd.)
• The PHP break statement breaks the execution of inner loop only.
<?php
for($i=1;$i<=3;$i++){
for($j=1;$j<=3;$j++){
echo "$i $j<br/>";
if($i==2 && $j==2){
break;
}
}
}
?>
OUTPUT:
11
12
13
21
22
31
32
33
PHP Break: inside switch statement
<?php
$num=200;
switch($num){
case 100:
echo("number is equals to 100"); OUTPUT:
break; number is equal to 200
case 200:
echo("number is equal to 200");
break;
case 50:
echo("number is equal to 300");
break;
default:
echo("number is not equal to 100, 200 or 500");
}
?>
PHP Break: with array of string
<?php
//declare an array of string
$number = array ("One", "Two", "Three", "Stop", "Four");
foreach ($number as $element) {
if ($element == "Stop") {
break;
}
echo "$element </br>";
}
?>
OUTPUT:
One
Two
Three
PHP Continue
• The PHP continue statement is used to continue the loop.
• The continue statement can be used with all types of loops such
as - for, while, do-while, and foreach loop.
OUTPUT:
11
22
33
PHP continue Example in while loop
<?php
//php program to demonstrate the use of continue statement
OUTPUT:
One
Two
Three
Four