linuxStat PHP Documentation

Repeated execution of PHP code

The PHP while statement provides an alternative mechanism
to repeatedly execute a block of PHP code.

The while statement has the following structure:

while (condition 1) {
   PHP Code 1
}

Condition 1 enclosed within the parentheses ( ) determines how many
times PHP Code 1 enclosed within the braces { } is executed.

The PHP code interpreter first tests condition 1.
If condition 1 is TRUE then PHP Code 1 is executed.
If condition 1 is FALSE then execution of the while statement ends.

After executing PHP Code 1, the PHP interpreter repeatedly
executes the following sequence:

condition 1
PHP Code 1
As long as condition 1 is TRUE, execution of the above sequence
continues.

If and when condition 1 becomes FALSE, execution of
the while statement ends.

Thus, if condition 1 never becomes FALSE, execution of
the while statement continues for ever.

Example 11.1:
$age = 12;             // Line 1
while ($age < 21) {    // Line 2
   if ($age < 13) {    // Line 3
     print "Age: $age. Marvin K. Mooney, you are a preteen. <br>";   // Line 4
     print "<br>";    // Line 5
   }
   elseif ($age < 20) {    // Line 6
     print "Age: $age. Marvin K. Mooney, you are a teenager. <br>";
   }
   else {
     print "<br>";
     print "Age: $age. Marvin K. Mooney, you are an adult. <br>";
   }
   $age++;    // Line 7
}

Line 1 assigns 12 to the integer variable $age.
Since the condition within the parentheses ( ) in Line 2 is TRUE
the PHP code after the left brace { in Line 2 is executed.
Since the condition within the parentheses ( ) in Line 3 is TRUE
the print statements in Line 4 and Line 5 are executed.

After that Line 7 is executed which increments $age to 13.
This completes what is known as the first iteration of the while
statement. The second iteration of the while statement begins
by testing the condition within the parentheses ( ) in Line 2.
Since the condition is still TRUE, the PHP code
after the left brace { in Line 2 is executed.

This time the condition within the parentheses ( ) in Line 3 is FALSE.
So the condition within the parentheses ( ) in Line 6 is tested.
Since the condition is TRUE, the PHP code
after the left brace { in Line 6 is executed.

After that Line 7 is executed which increments $age to 14.

The sequence of iterations continues until $age becomes 21. At this
point the condition within the parentheses ( ) in Line 2 is FALSE.
This ends the while statment.


The PHP program in Example 11.1 in an HTML document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<title>Executed on Dell Server, Intel Processor, created on HP laptop</title>
</head>
<body bgcolor="#6B7E23" text="#FFFF00" lang=EN-US>
<?php
# Example of how the while statement works.
$age = 12;             // Line 1
while ($age < 21) {    // Line 2
   if ($age < 13) {    // Line 3
     print "Age: $age. Marvin K. Mooney, you are a preteen. <br>";   // Line 4
     print "<br>";    // Line 5
   }
   elseif ($age < 20) {    // Line 6
     print "Age: $age. Marvin K. Mooney, you are a teenager. <br>";
   }
   else {
     print "<br>";
     print "Age: $age. Marvin K. Mooney, you are an adult. <br>";
   }
   $age++;    // Line 7
}
?>
</body>
</html>

Click the following link to execute the above PHP program
by my server's PHP interpreter.

Try the above example   Next   Index