You are on page 1of 6

Practical No 03: PHP Looping Structures

Practical Outcomes (PrOs):


Write a PHP program to demonstrate the use of Looping structures using -
a. while statement.
b. do-while statement.
c. for statement.
foreach statement.

A. Practical Significance
Often when code is written, the same block of code to run over and over again for a certain number
of times. So, instead of adding several almost equal code-lines in a script, we can use loops. Loops
are used to execute the same block of code again and again, as long as a certain condition is true.

B. Minimum Theoretical Background


1. The while loop
The while loop executes a block of code as long as the specified condition is true.
Syntax:
while (condition is true) {
code to be executed;
}
Example:
echo "Even numbers: </br>";
$i = 2;
while($i <= 10) {
echo "The number " . $i . "</br>";
$i = $i + 2;
}

2. The do-while loop


The do-while loop will always execute the block of code once, it will then check the condition,
and repeat the loop while the specified condition is true.
Syntax:
do {
code to be executed;
} while (condition is true);
Example:
echo "Odd numbers: </br>";
$i = 1;
do {
echo "The number " . $i . "</br>";
$i = $i + 2;
} while($i <= 10);
3. The for loop
The for loop is used when you know in advance how many times the script should run.
Syntax:
for (initialization; condition is true; increment/decrement) {
code to be executed for each iteration;
}
Example:
echo "Table of 12: </br>";
for($i=1; $i<=10; $i++)
{
echo $i * 12 . "</br>";
}
4. The foreach 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;
}
Example:
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}

C. Resources used
Sr. Instrument/Machine
Specifications Quantity Remark
No. /Software
Windows 8.1 (64-Bit), Intel (R) Core
1 Desktop PC 01 Nil
(TM) i3 CPU @2.40 GHz, 4GB RAM
Xampp v3.2.4, Package contains
2 Xampp 01 Nil
Apache + MariaDB + PHP + Perl
Notepad++ and
3 Editor and Browser 01 and 01 Nil
Google Chrome

D. Program Code
a) Write a php program to find the sum of 1 to n numbers using while loop.
<!DOCTYPE html>
<html>
<head>
<title> while loop </title>
</head>

<body>
<?php
$n = 10;
$i = 1;

while($i <= $n) {


$sum = $sum + $i;
$i++;
}
echo "Sum of $i to $n is $sum. </br>";
?>
</body>
</html>

b) Write a php program to display an array using foreach loop.


<!DOCTYPE html>
<html>
<head>
<title> foreach loop </title>
</head>

<body>
<?php
$arr = array(89, 45, 67, 83, 48, 29, 58, 74);
echo "Array Element: ";
foreach($arr as $val) {
echo $val . " ";
}
?>
</body>
</html>

c) Write a php program to display following pattern using for loops.


A
A B
A B A
A B A B
A B A B A

<!DOCTYPE html>
<html>
<head>
<title> foreach loop </title>
</head>

<body>
<?php
for($i=1; $i<=5; $i++) {
for($j=1; $j<=$i; $j++) {
if($j % 2 == 1)
echo "A ";
else
echo "B ";
}
echo "</br>";
}
?>
</body>
</html>

E. Result (Output of Code)


Program a): Sum of 1 to 10 is 55.
Program b): Array Elements: 89 45 67 83 48 29 58 74
Program c): pattern of A and B as shown in the question.

F. Practical related questions


a) Write a php program to display an associative array using foreach loop.
<!DOCTYPE html>
<html>
<head>
<title> foreach loop </title>
</head>

<body>
<?php
$subjects = array("1"=>"English", "2"=>"Hindi",
"3"=>"Sanskrut", "4"=>"Science", "5"=>"Mathematics");
foreach($subjects as $value)
{
echo $value . "</br>";
}
?>
</body>
</html>

Output
English
Hindi
Sanskrut
Science
Mathematics
b) Describe foreach loop in php.
The foreach loop - Loops through a collection of elements for each element in that collection. 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 every loop iteration, the value of the current array element is assigned to $value and the array
pointer is moved by one, until it reaches the last array element.

G. Exercise:
a) Write a php program to display numbers between 1 to 100 which are divisible by 7.
<!DOCTYPE html>
<html>
<head>
<title> for loop </title>
</head>

<body>
<?php
for($i=1; $i<=100; $i++) {
if($i % 7 == 0) {
echo $i . " ";
}
}
?>
</body>
</html>

Output 7 14 21 28 35 42 49 56 63 70 77 84 91 98

b) Write a php program to check a number is Armstrong.


<!DOCTYPE html>
<html>
<body>
<?php
$num = 153;
$i = $num;
$sum = 0;

while($i != 0) {
$digit = $i % 10;
$sum = $sum + ($digit ** 3);
$i = $i / 10;
}
if($sum == $num) echo "Armstrong number.";
else echo "Not Armstrong number.";
?>
</body>
</html>

Output Armstrong number.

Assessment Scheme

Performance Indicator Weightage

Process Related (30 Marks) 60 %


Write appropriate code to generate desired output
1 30 %
in Web application.
2 Debug, Test and Execute the programs 20 %

3 Following Ethical Practice 10 %

Product Related (20 Marks) 40 %

4 Presentation of Output 20 %

5 Timely Submission of Practical Assignment 10 %

6 Able to answer the sample questions 10 %

Total (50 Marks) 100 %

List of Students / Team Members


1) ………………………………………………………………………………….
2) ………………………………………………………………………………….
3) ………………………………………………………………………………….
4) ………………………………………………………………………………….

Assessment
Date Signature of
Marks Obtained
Subject Teacher
Process Related Product Related
Total (50 Marks)
(30 Marks) (20 Marks)

You might also like