0% found this document useful (0 votes)
8 views18 pages

Lecture 32 PHP Loops

This document covers PHP loops, including types such as while, do...while, for, and foreach, along with examples of each. It also discusses the break and continue statements used in loops. The lesson aims to enable students to apply different kinds of loops to solve problems effectively.

Uploaded by

good9761931226az
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views18 pages

Lecture 32 PHP Loops

This document covers PHP loops, including types such as while, do...while, for, and foreach, along with examples of each. It also discusses the break and continue statements used in loops. The lesson aims to enable students to apply different kinds of loops to solve problems effectively.

Uploaded by

good9761931226az
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

PHP LOOPS

LESSON NO.: 32
COURSE NAME: ADVANCED WEB TECHNOLOGY
COURSE CODE: E1UA407C
INSTRUCTOR NAME: DR. PRAMOD SONI
DURATION: 50 MINUTES
DATE OF CONDUCTION OF CLASS: 5.06.2025
1
Galgotias University
REVIEW OF THE KEY CONCEPTS OF LESSON NO. # 31

✔ if statement: executes some code if one condition is true.


✔ if else statement: executes some code if a condition is true and another
code if that condition is false
✔ if else if statement: executes different codes for more than two
conditions.

Galgotias University 2
“Imagine you have 100 names to print on a webpage.
Would you manually write 100 statements, or is there a
smarter way to automate this?”

Galgotias University 3
It refers to a process that Answer is USING
repeats itself until a
specific condition is met.
LOOPS
They help automate
repetitive tasks efficiently.

Galgotias University 4
LEARNING OUTCOMES

Outcome: Apply different kinds of loop to solve


problem

Galgotias University 5
PHP LOOPS

In PHP, we have the following loop types:

1. while - loops through a block of code as long as the specified


condition is true
2, do...while - loops through a block of code once, and then repeats
the loop as long as the specified condition is true
3. for - loops through a block of code a specified number of times
4. for each - loops through a block of code for each element in an
Galgotias University 6
PHP WHILE LOOP
The while loop executes a block of code as long as the specified
condition is true.
OUTPUT:
Example:
1 2 3 4 5
$i = 1;
while ($i < 6) {
echo $i;
$i++;
}
Galgotias University 7
PHP DO….WHILE LOOP

The do...while loop will always execute the block of code at least
once, it will then check the condition, and repeat the loop while
the specified condition is true.

Example: OUTPUT:
$i = 1;
1
do {
echo $i;
$i++;
} while ($i < 2);

Galgotias University 8
PHP FOR LOOP

The for loop - Loops through a block of code a specified number of


times.
OUTPUT:
Example:
<?php
The number is: 0
for ($x = 0; $x <= 5; $x++) { The number is: 1
echo "The number is: $x <br>"; The number is: 2
} The number is: 3
?> The number is: 4
The number is: 5

Galgotias University 9
PHP FOREACH LOOP

Loops through a block of code for each element in an array or


each property in an object.

<?php OUTPUT:
red
$colors = array("red", "green", "blue", "yellow");
green
foreach ($colors as $x) { blue
echo "$x <br>"; yellow
}
?>

Galgotias University 10
PHP BREAK

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

Example:
OUTPUT:
<?php The number is: 0
for ($x = 0; $x < 10; $x++) { The number is: 1
if ($x == 4) { The number is: 2
break; The number is: 3
}
echo "The number is: $x <br>";
}
?>
Galgotias University 11
PHP CONTINUE

The continue statement stops the current iteration in the for loop
and continue with the next.

Example: OUTPUT:
<?php The number is: 0
for ($x = 0; $x < 5; $x++) { The number is: 1
if ($x == 2) { The number is: 3
continue; The number is: 4
}
echo "The number is: $x <br>";
}
?>
Galgotias University 12
LEARNING ACTIVITY
Write a Program to create given pattern with * using for
loop

Galgotias University 13
REFLECTION OF LEARNING ACTIVITY
<?php
for($row = 1; $row <= 8; $row++)
{
for ($star = 1;$star <= $row; $star++)
{
echo "*";
}
echo "<br>";
}
?>
Galgotias University 14
SUMMARY

In this lecture, we have learnt the following loops:


 while - loops through a block of code as long as the specified condition is
true
 do...while - loops through a block of code once, and then repeats the
loop as long as the specified condition is true
 for - loops through a block of code a specified number of times
 foreach - loops through a block of code for each element in an array
 break statement - used to jump out of different kind of loops.
 continue statement - stops the current iteration in the for loop and
continue with the next.
Galgotias University 15
ATTAINMENT OF LOS IN ALIGNMENT TO THE
LEARNING ACTIVITIES

Apply different kinds of loop to solve problem

Galgotias University 16
INFORMATION ABOUT THE NEXT LESSON

✅ PHP Switch Statement


✅ PHP functions

17

Galgotias University
REVIEW AND
REFLECTION
FROM STUDENTS

Galgotias University 18

You might also like