Search This Blog

Tuesday, May 5, 2009

Loop constructs in PHP

hello Dear,

Firs sorry for waiting long time to post this since my last post. I was having lot of works to complete in last few days. herewith today we gonna look at loops in php

as in other languages php has loop/iterative construct. those loop construct are
  • for
  • while
  • do .. while
  • foreach
here is the sample for each of the above loops.

for loop ..

for($i=0; $i < 10; $i++)
{
echo ($i . '< br />');
}

//----------------------------------------------

While Loop

while($row=mysql_fetch_array($result))
{

echo ($row[0] . "< br />");

}

//----------------------------------------------

Do While loop

$i=0;
do
{
$i++;

echo ("the no is:" . $i . "< br />");

}
while($i<5);


//------------------------------------------------

foreach loop

$persons= array("name"=>"mcDonald","city"=>"torento","country"=>"USA");

foreach($item=keys($persons ))
{
echo($persons[ $item] . "< br />");
}

we will try the case statement in next lesson and i'll stop @ here thanks gys n galls