PHP Looping


Looping statements in PHP are used to execute the same block of code a specified number of times. PHP has the following looping statements: The while Statement
The while statement will execute a block of code if and as long as a condition is true.
while ( condition )
  code to be executed;

The example prints the random number between 0 and 9 until the random number matches the input value:
  • Compare the web input to character digits instead of integer digits.

  • TRUE and FALSE are a boolean type of PHP.

  • The rand(min,max) function returns a random number between min and max, inclusively.

  • break ends execution of the current for, foreach, while, do-while or switch structure.
 <html><body>
 <?php
  $input = ;
  if ( ( $input < '' ) ||
       ( $input > '' ) )
   echo "$input is not a digit!";
  else {
   while ( true ) {
    $rand = rand( 0, 9 );
    if ( $input == $rand ) {
     echo "Found the random $rand.";
     break;
    }
    else
     echo "The random is $rand.";
   }
  }
 ?>
 </body></html>
   




      I wrote a song, but I can’t read music so I don’t know what it is.    
      Every once in a while I’ll be listening to the radio and I say,    
      “I think I might have written that.”    
      — Steven Wright