In programming, loops are used to execute the same lines of code multiple times. The simplest type of loop is called a while loop. The lines of code in the body of a while loop will continue executing over and over WHILE the condition is true. The syntax looks like this:

while (condition) {
   //run this code repeatedly while the condition is true
}

Just like with if statements, the condition is evaluated as a boolean. If the condition is true, the block of code is executed and then it starts over my evaluating the condition again. If the condition is false, the block of code is not executed and the loop is terminated. Let's try making a loop that will print all the numbers from 1 to 10:

$i = 1;
while ($i <= 10) {
   print "The number is $i<br />";
   $i = $i + 1;
}

We start with a variable $i that we have set at 1. 1 is less than or equal to 10 so the loop body executes. "The number is 1<br />" is printed to the HTML, and then we add 1 to the variable $i. Now the condition is checked again, but this time $i is 2. 2 is still less than or equal to 10, so the loop body executes again. This continues and each time the number is printed and 1 is added to $i. Once $i gets to 11, the condition is no longer true, so the loop terminates.

A word about incrementing

When dealing with loops it is common to have an indexing variable, like $i in the previous example. Usually you will want $i to either increase by one or decrease by one on each iteration of the loop. In the previous example, we used $i = $i + 1; to do this, but there is a simpler way that accomplishes exactly the same thing:

++$i;

This is called the pre-increment operator. There is also a pre-decrement operator that will subtract one from the variable:

--$i;

Important Note: A common misconception is that ++$i is the same thing as $i + 1. They are not the same. ++$i actually changes the value of $i to be 1 greater. $i + 1 keeps $i as is, but gives you the number 1 greater than $i to use as a value.

With that said, we can rewrite our loop above using the pre-increment operator:

$i = 1;
while ($i <= 10) {
   print "The number is $i<br />";
   ++$i;
}

This loop accomplishes exactly the same thing as the previous one.

More Looping Examples

Looping can be used to accomplish a lot of different things.

In mathematics, the factorial of a positive non-decimal whole number is the product of all positive non-decimal numbers less than or equal to it. For example:
5 Factorial = 1 * 2 * 3 * 4 * 5 = 120

We can use a while loop to calculate the factorial of a number:

$i = 5; //the number to calculate the factorial of
$factorial = 1;
while ($i > 0) { //once $i is no longer a positive integer, stop looping
   $factorial = $factorial * $i;  //multiply by the current number
   --$i;  //go down to the next number
}
print $factorial;

This will print the number 120, the factorial of 5. If we want 10 factorial, all we have to do is change the initial value of $i, and our code will do the rest.

Now let's apply what we learned in PHP #2: GET and POST to ask a user what number they would like to know the factorial of. First we'll need an html form where the user enters the number:

<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<form method="get" action="factorial.php">
     Calculate the factorial of:<input type="text" name="factorial" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Now we'll create a php file called factorial.php that will process the info from the form using the same method as before.

<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<?php
$i = $_GET['factorial']; //the number to calculate the factorial of
$factorial = 1;
while ($i > 0) { //once $i is no longer a positive integer, stop looping
   $factorial = $factorial * $i;  //multiply by the current number
   --$i;  //go down to the next number
}
print "The factorial is $factorial.";
?>
</body>
</html>

Now view the html page in your browser and you should have a functioning factorial calculator. You can download the full source code for this example here:
Factorial Example
Factorial Example
Size: 1.17 KB

Be Sociable, Share!