Introduction to PHP – Part 5

advertisement

advertisement

Loops

A loop is a programming construct that facilitates programmer to execute some code number of times or a specified condition is true. For instance, we need to read a file stored in computer and print it in screen. Suppose the function available to read a file reads one line at a time and the file contains many lines, we have to write the same line of code many times to read one line at a time from the file.  By using loops we can execute the same code number of times called iterations to read the whole file, one line at a time until there is no line left in the file. A loop always needs to have a terminating condition, otherwise it will loop infinitely, and we need to kill the whole process to break the loop explicitly. In the above example, the terminating condition might be end of file. Basically, there are two types of loop in programming while and for.

All the examples presented in this tutorial prints multiplication table of 2 using different loops. The output of all examples looks like:

Multiplication Table of 2
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20

while loop

while loop examines the condition first and then execute the code if the condition is true. After the statements inside the loop are executed, the condition is again checked and the loop continues until the specified condition is false.  

Syntax:

while (condition)
{
          code block;
}

Example#1

In this example, we created a loop that starts with x=1 and continue as long as x is less than or equal to 10. In each loop iteration, x is increased by 1. The final value of x will be 11.

<html>
<body>
<?php
$x=1;
echo "Multiplication Table of 2 </br>";
while($x<=10)
{
  	echo "2 X " . $x . " = ". 2*$x. "<br />";
  	$x++;
}
?>
</body>

do while loop

do while loop execute the code first and then examines the condition. After the statements inside the loop are executed, the condition is checked and the loop continues until the specified condition is false.

Syntax:

do
{
          code block;
} while (condition);

Example#2

In this example, the  loop starts with x=1 and execute the first loop iteration without checking the condition. The condition is then checked when x becomes 2 and continue as long as x is less than or equal to 10. In each loop iteration, x is increased by 1. The final value of x will be 10.

<html>
<body>
<?php
$x=1;
echo "Multiplication Table of 2 </br>";
do
{
  	echo "2 X " . $x . " = ". 2*$x. "<br />";
  	$x++;
} while($x<=10)

?>
</body>

 
for loop:

A for loop contains three parts, initialization, condition and increment. Initialization is used to initialize a counter for the number of loop. This expression is executed before each loop iteration. Condition expression is a terminating condition, which is checked every time the loop is executed. If the conditional expression returns false, the loop terminates. Increment is used to increment the counter. After each loop iteration, this expression is executed.   

Syntax:

for(initialization; condition; increment)
{
          code block;
}

Example#3

This example demonstrate a for loop which starts from 1 to 10. Before each loop iteration, the condition is checked to see if the value of x is less than or equall to 10. In each loop iteration, x is increased by 1. Can you check, what will be the final value of x?

<html>
<body>
<?php
echo "Multiplication Table of 2 </br>";
for($x=1; $x<=10; $x++)
{
  	echo "2 X " . $x . " = ". 2*$x. "<br />";
}
?>
</body>

foreach loop:

Array is a systematic collection of data of similar data types. We will discuss array in detail in later tutorials. foreach loop is used to loop throughout the array

Syntax:

foreach($array as $value)
{
          code block;
}

Example#4

This example creates a array containing 10 numbers from 1 to 10. The loop is used to access the elements of the array. In each iteration, the current array element is assigned to the variable $value.

<html>
<body>
<?php
$numberArray = array(1,2,3,4,5,6,7,8,9,10);
echo "Multiplication Table of 2 </br>";
foreach($numberArray as $x)
{
  echo "2 X " . $x . " = ". 2*$x. "<br />";
}
?>
</body>

 

 

 

Download our latest pass4sure 70-685 dumps and 156-215.70 demos for ensuring your guaranteed success in real HP0-S26 exam.

About the Author:

Bhoj R Dhakal is a programmer. He has few years of experience in software development and maintenance. He has published series of PHP tutorials for webanddesigners.