0% found this document useful (0 votes)
26 views

Loops

There are several types of loops in PHP including while, do-while, for, and foreach loops. While loops continuously execute as long as a condition is true, do-while loops guarantee running at least once, for loops are used when the number of iterations is known, and foreach loops iterate over elements in an array. The break and continue statements can be used to control loop execution flow.

Uploaded by

Sunaina Arora
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Loops

There are several types of loops in PHP including while, do-while, for, and foreach loops. While loops continuously execute as long as a condition is true, do-while loops guarantee running at least once, for loops are used when the number of iterations is known, and foreach loops iterate over elements in an array. The break and continue statements can be used to control loop execution flow.

Uploaded by

Sunaina Arora
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Loops in php

While loop
while loops are the simplest type of loop in PHP.
The basic form of a while statement is:
while (condition)
statement
While loop continued….
The value of the expression is checked each time
at the beginning of the loop, so even if this value
changes during the execution of the nested
statement(s), execution will not stop until the end
of the iteration (each time PHP runs the
statements in the loop is one iteration).
For example
<
? php
$i = 1;
while ($i <= 10) {
echo $i++;
}
?>
Try it out
<?php
$i = 0
while ($i < 3)
{
$i++;
}
print $i;
?>c) 0
d) 1
a) 2
b) 3
Correct answer
a) 2
b) 3
c) 0
d) 1
Explanation: The increment happens and then
the check happens
<?php
$i = 0
while ($i++)
{
print $i;
}
print $i;
?>
a) 0
b) infinite loop
c) 01
d) 1
Correct answer
a) 0
b) infinite loop
c) 01
d) 1
Explanation:As it is a post increment, it checks
and then does not enter the loop, thus prints
only 1
Do-while

The main difference from regular while loops is


that the first iteration of a do-while loop is
guaranteed to run (the truth expression is only
checked at the end of the iteration)
syntax
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
• The above loop would run one time exactly, since
after the first iteration, when truth expression is
checked, it evaluates to false ($i is not bigger
than 0) and the loop execution ends.
Question for you
<?php
$i = 5;
while (--$i > 0 && ++$i)
{
print $i;
}
?>
a) 5
b) 555555555…infinitely
c) 54321
d) error
Correct answer is
• a) 5
b) 555555555…infinitely
c) 54321
d) error

Explanation: As it is && operator it is being


incremented and decremented continuously
For loop
The for loop is used when we know in advance
how many times the code should run.
Syntax:
for (init counter; test counter; increment
counter) {
code to be executed for each iteration;
}
All three parameters are optional, but semicolon (;)
is must to pass in for loop. If we don't pass
parameters, it will execute infinite.
<?php
$i = 1;
//infinite loop
for (;;) {
echo $i++;
echo "</br>";
}
?>
Nested for loop
We can use for loop inside for loop in PHP, it is known
as nested for loop. The inner for loop executes only
when the outer for loop condition is found true.
<?php
for($i=1;$i<=3;$i++){
for($j=1;$j<=3;$j++){
echo "$i $j<br/>";
}
}
?>
Solve it
<?php
$NUM1 = 3;
$NUM2 = 4;
var_dump($NUM1<>$NUM2);
?>
a)bool(true)
b)bool(false)
c)Error
d)None of the above
a)bool(true)
b)bool(false)
c)Error
d)None of the above

Explanation: The above code will


print bool(true) on the webpage,
the <> operator is "not equal to" operator.
For-each loop
The foreach loop works only on arrays, and is
used to loop through each key/value pair in an
array.
Syntax:
foreach ($array as $value) {
code to be executed;
}
For example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $x => $val) {
echo "$x = $val<br>";
}
?>
Output :
Peter = 35
Ben = 37
Joe = 43
Example 2
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
Output:
red
green
blue
yellow
PHP Break

The break statement can also be used to jump out of a loop.


<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?>
Output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
Php continue
The continue statement breaks one iteration (in the loop), if
a specified condition occurs, and continues with the next
iteration in the loop.
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
?>
Output will be:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9

You might also like