Thread: Looping in PHP
View Single Post
Banned
18Jun2009,14:47  
gkumar's Avatar
In programming it is often necessary to repeat the same block of code a given number of times, or until a certain condition is met. This can be accomplished using looping statements. PHP has two major groups of looping statements: for and while. The For statements are best used when you want to perform a loop a specific number of times. The While statements are best used to perform a loop an undetermined number of times. In addition, you can use the Break and Continue statements within looping statements.
exemples:-

The While Loop
Code:
            while (condition)
              {
                  code needed to loop;
               }
The Do...While Loop
Code:
      do
          {
              code needed to loop;
          }
           while (condition);
The For Loop
Code:
 
       for (initialization; condition; increment)
               {
                  code needed to loop;
                }
The Foreach Loop
Code:
        foreach (array as value)
            {
                code needed to loop;
            }
Break and Continue Statements
PHP Code:
           <?php
             
for ($i 0$i <= 10$i++)
                    {
                        if(
$i==5)
                            {
                                break;
                             }
            echo 
"The number is " $i "<br/>";
            }
          
?>

Last edited by shabbir; 18Jun2009 at 15:25.. Reason: Code blocks