Monday, May 30, 2011

PHP - While Loop

The function of the while loop is to do a task over and over as long as the specified conditional statement is true. This logical check is the same as the one that appears in a PHP if statement to determine if it is true or false. Here is the basic structure of a PHP while loop:

Syntax :-

while ( conditional statement is true){  //do this code; }

This isn't valid PHP code, but it displays how the while loop is structured. Here is the break down of how a while loop functions when your script is executing:

  1. The conditional statement is checked. If it is true, then (2) occurs. If it is false, then (4) occurs.
  2. The code within the while loop is executed.
  3. The process starts again at (1). Effectively "looping" back.
  4. If the conditional statement is false, then the code within is not executed and there is no more looping. The code following the while loop is then executed like normal.

$brush_price = 5;
$counter = 10;
echo "";
echo "";
echo "";
while ( $counter <= 100 )
{ echo ""; $counter = $counter + 10; } echo "
QuantityPrice
"; echo $counter; echo ""; echo $brush_price * $counter; echo "
";

Output : -

QuantityPrice
1050
20100
30150
40200
50250
60300
70350
80400
90450
100500

No comments:

Post a Comment