WBP22202A0023PR03
WBP22202A0023PR03
Experiment No: 3
Title of Experiment Write a PHP program to demonstrate the use of looping structures using:
a) While statement
b) Do-while statement
c) For statement
d) Foreach statement
Ans: A for loop will terminate when the condition specified in the loop evaluates to false.
☆ Exercise
Ans:
<?php
echo "<h3>Even Numbers from 1 to 10</h3>";
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 == 0) { // Check if number is even
echo "$i is even <br>";
}
}
?>
Output:
Ans:
1.Increment
<?php
$n = 5; // Number of rows
for ($i = 1; $i <= $n; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br>";
}
?>
2.Decrement
<?php
$n = 5; // Number of rows
for ($i = $n; $i >= 1; $i--) {
for ($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br>";
}
?>
Output: