Tuesday, 21 July 2015

PHP Loops

Loops are some kind of flow control statements in PHP. It supports to execute some set of statements recursively, based upon the conditional statement provided. There are various type of loop statements in PHP.
  1. while
  2. do..while
  3. for
Each of the above statements are going to perform same function but varied with the way of it’s execution, syntax or structure. Let us see about these statements one by one in detail.
php_loop_control

while

while statement has three part of its structure. That is, initialization, conditional expression and statement to handle increments or decrements. This is shown as follows.
initialization
while(condition) {
 //set of expressions to be execute recursively
 ...
 increment (or) decrement
}
In initialization part of above structure, we need to assign a value to loop index. Then, this value will be repeatedly check with the given conditional statement. Once the condition is satisfied, then loop will be executed which is called as iteration. For each iteration, the loop index should be incremented or decremented based on the condition. Otherwise, loop will fall into infinite execution. Let us see a program for an example of while loop to print the numbers 1 to 3.
<?php

$i=1;
while($i <= 3) {
 echo $i;
 $i++;
}
?>

do..while

do..while statement is also has three parts, as discussed in while statement. But it varies with it’s guaranteed execution in first iteration of the loop whether the condition satisfied or not. Why because, the loop condition will be checked at the end of the loop statement. So the syntax will be as follows.
initialization
do {
 //set of expressions to be execute recursively
 ...
 increment (or) decrement
}while(condition)
The following program will print 1 to 3, even though the condition is to print numbers greater than 1
<?php
$i = 1;
do {
echo $i;
$i++;
} while (($i > 1) && ($1 <= 3)); ?>

for

for loop structure will facilitate the developer for easy understanding of code flow. Because, it holds all parts of a loop statement, that is, initialization, condition and increments/decrements part in one single statement. The syntax of for loop structure is as shown as below.
for(initialization; conditional statement; increments (or) decrements) {
//set of expressions to be execute recursively
}
The while loop example shown above is converted as for loop as below
<?php
for($i=1;$i <= 3;$i++) {
echo $i;
}
?>
Even though this form is easy to understand by reduced number of lines of code comparatively, the use of while loops is the good programming practice. Why because, any type of loop structure will be translated to ‘while’ loop format at the time of compilation.
Apart from the above types of loop structure, PHP supports some other statement to handle loop. That is foreach statement as shown as below.
foreach($array as $value_array) {}
(or)
foreach($array as $key_array=>$value_array) {}
The later syntax of foreach is used to organize an array with the key,value pair. And, the former one is used to move the array pointer one step inner level of an multidimensional array.

0 comments:

Post a Comment