CHAPTER 3 ?– PHP BASICS 101
The switch Statement
You can think of the switch statement as a variant of the if-else combination, often
used when you need to compare a variable against a large number of values:
switch($category) {
case "news":
echo "
What's happening around the world
";
break;
case "weather":
echo "
Your weekly forecast
";
break;
case "sports":
echo "
Latest sports highlights
";
break;
default:
echo "
Welcome to my Web site
";
}
?>
Note the presence of the break statement at the conclusion of each case block. If a
break statement isn??™t present, all subsequent case blocks will execute until a break statement
is located. As an illustration of this behavior, let??™s assume that the break statements
are removed from the preceding example and that $category is set to weather. You??™d get
the following results:
Your weekly forecast
Latest sports highlights
Welcome to my Web site
Looping Statements
Although varied approaches exist, looping statements are a fixture in every widespread
programming language. This isn??™t a surprise because looping mechanisms offer a
simple means for accomplishing a commonplace task in programming: repeating a
sequence of instructions until a specific condition is satisfied. PHP offers several such
102 CHAPTER 3 ?– PHP B ASICS
mechanisms, none of which should come as a surprise if you??™re familiar with other
programming languages.
Pages:
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187