Web Info and IT Lessons Blog...

Wednesday 12 November 2014

Loops in PHP

The previous lesson of PHP lessons series was about PHP Arrays and in this lesson we will learn PHP Loops. A loop is a procedure which repeats itself after some time until a certain condition for the termination is reached. In PHP there are four types of loops. Two for loops and Two while loops i.e

1. for loop
2. foreach loop
3. while loop
4. do while loop

PHP Loops

For loop in PHP


The basic structure of a for loop is given below:


for(initialize counter; test condition; increment or decrement counter){
 // code to run 
}

The counter in a for loop is a variable usually initialized with a zero value, then there is a condition check on the counter and if the condition is true then the value of counter is incremented. The example code of a for loop given below will echo the text "Hello World" ten times:


for($i=0;$i<10;$i++){
 echo 'Hello World';
}

In the above code when the first time the for loop starts the value of $i will be initialized with 0 and the condition on the counter $i will be checked. If the value is less than 10, the value of $i will be incremented by 1 and the code to echo 'hello world' will run. Now the second iteration of the loop will start and this time the value of $i be equal to 1 as it was incremented in the previous iteration but still the value 1 is less than 10 so the value of $i will be incremented by 1 again and the code to echo 'Hello World' will run again. Each time the condition on the counter becomes true the value of the counter will be incremented by 1 and there will come a time when the value of counter will become 10, the condition will become false and the loop will break.

Print the values of an array using for loop:

For loop can be used to print the values at all the indexes of an array. The counter of for loop will iterate all the indexes of an array and print the values at each index. The example code of printing the values of an array is given below:


$numbers=array("One","Two","Three");

for($i=0;$i<3;$i++) {
  echo $numbers[$i].'
'; }

Foreach loop in PHP


Foreach loop in PHP is usually used to iterate and perform actions on an associative array as it is the only one of the four loops that can iterate an associative array. The basic structure of a foreach loop is shown below:


foreach (array as key){
 // run code
}

The above foreach loop will run the code inside the curly brakets { } for each value stored in the array. Foreach loop can iterate both indexed arrays and associative arrays.

Print the values of an indexed array using foreach loop:

The code given below will execute the echo statement for each value of the indexed array $marks.


$marks=array("35","37","43");
foreach ($marks as $key) {
  echo $key;
} 

Print the values of an associative array using foreach loop:

The code given below will execute the echo statement for each value of the associative array $marks.


$marks=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
foreach ($marks as $key) {
  echo $key;
} 

Also Read: PHP Arrays

While loop in PHP


The basic structure of a while loop is shown below:


while(condition true){
 // run code
}

The above structure of a while loop shows to run a code again and again until the condition is false. If the condition in while loop will never become false then the while loop will run forever and will execute the code inside the loop again and again. To avoid the code lock, be sure to provide such a condition in the while loop that will become false at some point. An example of code lock is given below:


$counter = 1;
while($counter < 10){
 echo 'Hello World';
}

The example code given above is an example of code lock in while loop because the counter will be less than 10 forever and 'Hello World' will be echoed infinite times. Now a valid example of a while loop would be like this:


$counter = 1;
while($counter < 10){
 echo 'Hello World';
 $counter++;
}

The only thing in the above code that makes it valid is that every time the code inside the while loop runs, the counter variable is incremented which means that in the ninth iteration of the while loop the counter value will become 10, the condition will become false and the loop will break.

Do While loop in PHP


The basic structure of a do while loop is shown below:


do{
 // run code
}while(condition true);

The only difference between a while and a do while loop is that in a while loop the condition of the loop is checked first and then the code inside the loop is executed but in a do while loop the code inside the loop is executed first and the condition of the loop is checked after that. This means that in a do while loop even if the condition is false the code will execute once before the condition is checked. An example of a do while loop is given below:


$counter = 1;
do{
 echo 'Hello World';
 $counter++;
}while($counter < 10);

No comments:

Post a Comment