While Loop in PHP

This is the basic PHP tutorial about while loop. Loops are used to repeat the same pattern multiple times.

Source Code 


<html>
 <head>
  <title>Loops: while</title>
 </head>
 <body>
 <?php /* while loops
 
   while(expression)
    statement;

   {} around multi-line statements (like if-statements)
   watch out for infinite loops
  */
  
  // This loop outputs 0-10
  $count = 0;
  while ($count <= 10) {
   echo $count . ", ";
   $count++;
  }
  echo "<br />Count: {$count}";
 ?>
 <br />
 <?php
  // Loops can be combined with if-statements
  $count = 0;
  while ($count <= 10) {
   if ($count == 5) {
    echo "FIVE, ";
   } else {
    echo $count . ", ";
   }
   $count++;
  }
  echo "<br />Count: {$count}";
 ?>
 
 </body>
</html>

Output of the Program 

While Loop in Php

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment