Lecture 32 PHP Loops
Lecture 32 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
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
Galgotias University 5
PHP LOOPS
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
Galgotias University 9
PHP FOREACH LOOP
<?php OUTPUT:
red
$colors = array("red", "green", "blue", "yellow");
green
foreach ($colors as $x) { blue
echo "$x <br>"; yellow
}
?>
Galgotias University 10
PHP BREAK
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
Galgotias University 16
INFORMATION ABOUT THE NEXT LESSON
17
Galgotias University
REVIEW AND
REFLECTION
FROM STUDENTS
Galgotias University 18