Loops in php

The term Loops is used when there is repetition involved in your php program. Means, you want to execute the same block of code to the number of times.

Normally php has four types of loop
  1. for loop
  2. while loop
  3. do while loop
  4. foreach loop

for loop:-

for loop is used when you know exactly how long you want to run the code segment, let's have a look over following program.

<?php
for($i=1;$i<6;$i++)

{

echo "Hello World";

}?>

Output will be:-

Hello WorldHello WorldHello WorldHello WorldHello World

Yeah this is exactly what we will get, now we want to show every Hello World in new line, for this you should use html br tag or \n Escape Sequence. For example

for($i=1;$i<6;$i++)
{
echo "Hello World\n";
}

Let's Look and Understand For Loop Structure

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

First of all we will initialize our variable, in the above case it is $i=1 , then condition is applied, if condition will true then body of loop will run, means then code within body of loop will be executed and loop gets increment of one, if condition is wrong then loop will be terminated and nothing happen.

Clear your concept

Many people thinks that loops get increment before code to be executed in the body of loop, its totally wrong.

Note:- It's not must that loop will be always incremented by 1 , its depend upon you, you can make it happen for loop to be incremented 2 or no of times what you want, if you want from loop to be incremented 2 times then use this

i+2 instead of i++ becuase i++ means i=i+1

While Loop

While loop is used when you dont know how many times loop will be executed, also loop will only run if condition will true. Let's look over while loop structure and a sample example.

Example:-

<?php
$i=1;

while($i<6)

{

echo "Hello World";

$i++;

}
?>

You will be saying in your heart that in this program of while loop we know that loop will be executed 5 times, yes you are write but normally while loop in php is used when we don't know exactly how many times code will run for example we want to run the code no of times, according to records in database, now we don.t know how many times the loop will run. The above illustrated example was just to tell you the basic structure of while loop.

Do - While Loop

do while loop is exactly same as while loop but in this loop code in the body of loop runs atleast one time, no matter whether condition is true or false. let's have a look over it.

<?php
$i=1;

do {

$i++;

echo "Hello World";

}

while ($i<6); ?>

As you see that body of loop is executed first then condition is checked so if condition is wrong then loop will run atleast one.

Note:- Do while loop is used rarely in php

foreach loop

The foreach loop is used to get values from arrays. For every loop, the value of the current array element is assigned to $val and the array pointer will be moved by one and so, on.

<?php

$arr_name=array("a", "b", "c");

foreach ($arr_name as $val)
{
echo "Value is: " . $val;
}


?>

0 comments: