Loops In PHP

For Loop In PHP

When we want to execute a statement or set of statement more then one time then we use loop. We use for loop where we already know that ho many time these statements should be execute. For example if we want to print 10 number on screen then we do this without loop in the following way:

echo 1 2 3 4 5 6 7 8 9 10;

But if we want to print 1 to 10000 number then its take lot of time and work. The easiest way to do this is loop. With the help of loop we can execute a statement more the one time again and again. As we know that 10000 number will be print so we execute a statement which print 1 number at a time thousand time. So the number of execution already known then we use FOR LOOP.
FOR LOOP execute a statement or set of statement specific number of time which are already known.
Syntax of For Loop


for ( initial Value   ;   Final Value  ;  Increment )

{

Statement 1;

statement 2;

---

---

}


Where initial value is any expression mostly an variable which count the first value. Final value is an other expression which is logical test. This expression compare the initial counter with this condition if its true then execute the statement in the body of loop. After this control pass to increment expression which increase the initial counter specific time by default one time. after increment the counter its again compare with given condition if true then again execute the statements in body of loop and then again increase the counter this process repeated again and again while condition remain true. when condition give false result, then its terminate the loop and the control is transfer outside form the lop.
Now we take an example to make more understandable this tutorial.


<?PHP

$start = 1;

for($start; $start <= 10; $start++)

 {

echo  $start . "<BR>";

}

?>

First of all we declare an variable $start so we used following
statement:
$start = 1;
Now we set the loop
for($start; $start <= 10; $start++) {
So $start is initial counter which is 1, according to given condition our initial counter check if counter is less then 10 then loop executed the statement in the body of loop. So in our example initial counter is 1 which is less then 10 so the statement in the body of the loop will be execute.
Statement in the body of the loop print the value of $start on the screen. So in the result 1 is display on the screen and
tag give a line break.
After this control transfer to increment expression which $start++ which make increment in $start one time this is same as
$start=start+1;
Now the value of $start means our counter value is now 2. Now again control transfer to compression expression which compare the counter with final value.
These step repeat again and again as well as condition remain true. Our counter value increase one time in every step. when its reach to 11 then condition return False and loop will be terminate and control transfer to next statement after the body of loop but before the termination of loop we will get 1 to 10 number on screen as follow:
1
2
3
4
5
6
7
8
9
10