image


Valora:  2.5/5
Inicio » PHP » PHP Tutorial » Statements




Loops

Loops allow us to iterate group of instructions, that is to say, repeat the execution of a group of instructions as long as a condition is complied with.

Statement while
<?php
   while (condition)
   {
      execute statments.
   }
?>

As long as the condition is true, the execution of the instructions within the "while" statement will be repeated.

<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
   <title>PHP Example</title>
</head>
<body>
Start<BR>
<?php
   $i=0;
   while ($i<10)
   {
      echo "The value of i is ", $i,"<br>";
      $i++;
   }
?>

End<BR>
</body>
</html>

Execute View code

In the following example, at the beginning the value of $i is 0, during the execution of the loop, 1 is being added to the value of $i so that when the value of $i is 10 it does not comply with the condition and the execution of the loop is over.



Statement for
<?php
   for (initial ; condition ; execute in eiteration)
   {
      execute statments.
   }
?>

<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
   <title>PHP Example</title>
</head>
<body>
Start<BR>
<?php
   for($i=0 ; $i<10 ; $i++)
   {
      echo "The value of i is ", $i,"<br>";
   }
?>

End<BR>
</body>
</html>

Execute View code

The statement "for" is the most complete of loop statements. It allows us to control the whole operation of the loop with only one statement.

The first parameter of "for" is executed the first time and it is used to initiate the variable of the loop, the second parameter indicates the condition to be achieved so that the loop will continue executing, and the third parameter is a statement that executes at the end of every iteration and modifies the value of the iteration variable.






WebEstilo.com - Introduzca su e-mail y conozca las novedades. No hacemos Spam.
Google
  Web WebEstilo.com   
 
Valid HTML 4.01!
Última modificación:24 de Diciembre de 2004. Spain - España.
© 1998-2004 por Joaquin Gracia. Todos los derechos reservados.