PHP Lesson 09 - control structure, while loop

Started by Parham, September 03, 2003, 12:27:39 AM

Previous topic - Next topic

Parham

In the last lesson we discussed the "foreach" loop which in my opinion is the simplest type of loop.  A lot of people seem to agree though that the "while" loop is one of the easiest.  The difference between the "foreach" loop and the "while" loop is that the "foreach" loop is exclusively used for arrays while the "while" loop can be used in a number of different situations.

The "while" loop works on a very simple concept: while something evaluates to true, keep doing something else.  Because we're working on the basis of true and false, we will need our comparison operators (discussed in lesson 7).  If you can't quite remember, comparison operators compare values (or types) in two (or more) variables and return either "true" or "false".

Let's begin by looking at the syntax of the "while" loop:

while ( expression is true ) { do something }

the EASIEST way to show how the "while" loop works is to show you code right from the PHP documentation:


<?
$i = 1;
while ($i <= 5) {
  print "$i\n";
  $i += 1;
}
?>


This is by far the greatest example code for the "while" loop because it shows you the very essence of why it exists.  What we do first is declare a variable $i and give it an initial value of "1".  The next few lines say exactly this: "while the variable $i is less than or equal to 10, print the variable then add 1 to it".

And here is what PHP will do (pardon the very detailed explanation, but it's important):
-set $i to 1
-begin loop, $i is less than or equal to 10 (true), so do everything inside the block
-print $i (1)
-increase $i by 1 (now $i is 2)
-continue loop, $i is less than or equal to 10 (true), so do everything inside the block
-print $i (2)
-increase $i by 1 (now $i is 3)
-continue loop, $i is less than or equal to 10 (true), so do everything inside the block
-print $i (3)
-increase $i by 1 (now $i is 4)
-continue loop, $i is less than or equal to 10 (true), so do everything inside the block
-print $i (4)
-increase $i by 1 (now $i is 5)
-continue loop, $i is less than or equal to 10 (true), so do everything inside the block
-print $i (5)
-increase $i by 1 (now $i is 6)
-stop loop, $i is now greater than 5 (false), don't do anything inside the block and move on

Here is the output you will see:

"1
2
3
4
5"

You have to be VERY careful with the "while" loop because you may accidentally throw your program into an infinite loop by writing and expression that always yields a true answer.  PHP itself by default is set to stop a process which spans more than 30 seconds but if you accidentally get into an infinite loop on your web server, you will have some explaining to do to your administrator.  Here is an example (although very stupid) of code which will yield an infinite loop:


<?
$i = 1;
while ($i >= 1) {
  print "$i\n";
  $i += 1;
}
?>


This will print as many numbers (starting from 1) as it can before PHP decides to shut down the program by force.

To sum up the "while" loop: the "while" loop allows you to keep executing a chunk of code as long as some expression is true.

CapriSkye


[Unknown]

while (expression)
  statement;


Where expression is anything that can be true or false, such as "$value == 5" and statement; is any code or group thereof... to group it, use curly braces.

-[Unknown]

Parham

Quote from: CapriSkye on August 23, 2004, 07:39:34 PM
does php allow do while?

yes it does:


do {
//code
} while (expression);


the only difference before do..while and while loops is that a while loop will check the expression first whereas the do..while loop is guaranteed to work at least once because the code is executed and then the expression is checked the first time around.

this is a control structure i don't particularly like because it tends to confuse people.  You can also emulate it with a while() loop which more people tend to understand.

[Unknown]


CapriSkye

hehe unknown don't we all do?

php is so much like c++, just thought that they wouldn't leave do while out.
thanks for the tutorial parham.

Parham

Quote from: CapriSkye on August 25, 2004, 01:59:04 AM
hehe unknown don't we all do?

php is so much like c++, just thought that they wouldn't leave do while out.
thanks for the tutorial parham.

my pleasure

Advertisement: