For Loop
for ($i=0; $i<=2; $i++)
echo 'i = '.$i.'<br /><br />';
Results:
i = 0i = 1
i = 2
While Loop (may not b e executed at all)
$i=1;
while ( $i<4 )
{
echo 'i='.$i.'<br />';
$i++;
}
Results:
i=1i=2
i=3
Do..While Loop (always executed at least once)
$i=1;
do
{
echo 'i='.$i.'<br />';
$i++;
}
while ( $i<4 )
Results:
i=1i=2
i=3
foreach Loop
$ron2[0]='A';
$ron2[1]='B';
$ron2[2]='C';
foreach ($ron2 as $key => $value){
echo "the value at $key is $value.<br />";
}
Results:
the value at 0 is A.the value at 1 is B.
the value at 2 is C.
Breaking out of a loop or script
break; //goes to the next line after the loop
continue; // goes to the top of the loop and continues iteration
exit; //exits script
die;
Die Example 1: mysql_query($query) or die ('Could not execute query.');
Die Example 2:
function err_msg()
{
echo 'MySQL error was: ';
echo mysql_error();
}
mysql_query($query) or die (err_msg());