Low cost ecommerce web development India flash website design
We'll look at more complicated comparisons as the need arises. For the time being,
a general familiarity with the if-else statement is sufficient.
Another often-used PHP control structure is the while loop. Where the if-else
statement allowed us to choose whether or not to execute a set of statements
depending on some condition, the while loop allows us to use a condition to
determine how many times we'll execute repeatedly a set of statements. Here's
what a while loop looks like:
while ( condition ) {
// statement(s) to execute over
// and over as long as condition
// remains true
}
The while loop works very similarly to an if-else statement without an else
clause. The difference arises when the condition is true and the statement(s) are
executed. Instead of continuing the execution with the statement that follows
the closing brace (}), the condition is checked again. If the condition is still true,
then the statement(s) are executed a second time, and a third, and will continue
to be executed as long as the condition remains true. The first time the condition
evaluates false (whether it's the first time it's checked, or the one-hundred-and-
61
Control Structures
first), execution jumps immediately to the next statement following the while
loop, after the closing brace.
Loops like these come in handy whenever you're working with long lists of things
(such as jokes stored in a database... hint-hint!), but for now we'll illustrate with
a trivial example: counting to ten. This script is available as count10.php in the
code archive.
$count = 1;
while ($count <= 10) {
echo( "$count " );
$count++;
}
It looks a bit frightening, I know, but let me talk you through it line by line. The
first line creates a variable called $count and assigns it a value of 1. The second
line is the start of a while loop, the condition for which is that the value of $count
is less than or equal (<=) to 10. The third and fourth lines make up the body of
the while loop, and will be executed over and over, as long as that condition
holds true. The third line simply outputs the value of $count followed by a space.
The fourth line adds one to the value of $count ($count++ is a short cut for
$count = $count + 1—both will work).
So here's what happens when this piece of code is executed. The first time the
condition is checked, the value of $count is 1, so the condition is definitely true.
The value of $count (1) is output, and $count is given a new value of 2. The
condition is still true the second time it is checked, so the value (2) is output and
a new value (3) is assigned. This process continues, outputting the values 3, 4,
5, 6, 7, 8, 9, and 10. Finally, $count is given a value of 11, and the condition is
false, which ends the loop. The net result of the code is to output the string "1
2 3 4 5 6 7 8 9 10 ".
The condition in this example used a new operator: <= (less than or equal).
Other numerical comparison operators of this type include >= (greater than or
equal), < (less than), > (greater than), and != (not equal). That last one also
works when comparing text strings, by the way.
Another type of loop that is designed specifically to handle examples like that
above, where we are counting through a series of values until some condition is
met, is called a for loop. Here's what they look like:
for ( initialize; condition; update ) {
// statement(s) to execute over
// and over as long as condition
62
Getting Started with PHP
// remains true after each update
}
Here's what the above while loop example looks like when implemented as a for
loop:
for ($count = 1; $count <= 10; $count++) {
echo( "$count " );
}
As you can see, the statements that initialize and increment the $count variable
join the condition on the first line of the for loop. Although the code is a little
harder to read at first glance, having everything to do with controlling the loop
in the same place actually makes it easier to understand once you're used to the
syntax. Many of the examples in this book will use for loops, so you'll have plenty
of opportunity to practice reading them.
website designer freelance ASP PHP ecommerce web developer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110