Saturday, May 28, 2011

PHP For Loop

For loops are a pretty common loop in programming. This loops syntax is exactly like c, since PHP is c based. The loop below will print 0-9 on the screen.
<?php
for($i=0;$i<10;$i++)
{
    echo $i;
}
?>
There are a couple ways you can setup and call For Loops.
<?php
$i=0;

for(;$i<10;$i++)
{
    echo $i;
}
?>
 You can do an infinite loop also with the For Loop.
<?php
for(;;)
{
    echo "Hello\n<br/>";
}
?>
Loop through an array
<?php
$a = array("hello","world","how","are","you");

for($i=0;$i<sizeof($a);$i++)
{
    echo $a[$i]." ";
}
?>

No comments:

Post a Comment