case12:
echo"There are 31 days in this month.<br />";break;
case4:
case6:
case9:
case11:
echo"There are 30 days in this month.<br />";break;
case2:
echo"There are either 28 or 29 days in this month.
<br />";break;
default:
echo"I do not understand your month entry.";
}
?>
PHP case values can be strings:
switch($fruit){ case"apple":
echo"This is an apple.";
break;
case"orange":
echo"This is an orange.";
break;
case"banana":
echo"This is a banana.";
break;
default:
echo"This is not an allowed fruit treat.";
}
Comparisons against the value to be tested are case-sensitive. So, if
$fruit is assigned as $fruit = "Banana"; prior to the switch con- struct, (instead of$fruit = "banana";) the default message is printed. If this is a problem, it can be overcome by using the strtolower() or strtoupper()functions.
they can be written so that loop execution (or termination) is based on conditions that change while statements in the loop are being executed.
The former situation uses count-controlled loops and the latter uses condi- tional loops.
5.5.1 Count-controlled Loops
Count-controlled loops are managed with the for keyword. The general syntax of a count-controlled loop is:
for ($counter={expression giving on initial value of counter}; {expression giving high (or low) value of counter};
{expression controlling incrementing (or decrementing) of counter}){ {one or more statements to be executed inside loop}
}
The forkeyword is followed by three statements inside parentheses.
The first statement sets the initial value of a counter. You can give the identifier name—$counterin the above example—any name you like. The second expression sets conditions under which the loop should continue to execute, or to look at it another way, sets the terminating condition; the loop continues to execute as long as the value of the second expression istrue. The third expression controls how the counter is incremented or decre- mented. The counter is often incremented or decremented in steps of 1, but you can use other values as appropriate. It is up to you to make sure that these three related expressions are consistent and will actually cause the loop to terminate. For example, the loop
for ($i=1; $i=12; i+=2)
will never terminate because $iwill never equal 12. Perhaps you meant to write the second expressionas $i<=12;. If so, then the loop will execute for$i=1, 3, 5, 7, 9, and 11.
Now, consider Document 5.4, which displays the integers 0–10, in order.
The counter$kis initialized to 1. It is incremented in steps of 1, and the loop executes as long as$kis less than 10. Use of the shortcut incrementing or decrementing operators, as in$k++, is very common inforloops.
5.5 Loop Structures 101
Document 5.4 (counter.php)
<?php
echo"Here's a simple counter:<br />";
for($k=0;$k<=10;$k++) { echo $k."<br />";
}
?>
Document 5.5 shows a version of Document 5.4 which counts backward from 10.
Document 5.5 (countdown.php)
<?php
echo"Countdown…<br />";
for($k=10; $k>=0; $k−−) { echo $k."<br />";
}
echo"FIRE!!<br />";
?>
5.5.2 Conditional Loops
It is often the case that conditions under which repetitive calculations will or will not be executed cannot be determined in advance. Instead, conditions controlling the execution or termination of a loop structure will be deter- mined by values calculated inside the loop, while the script is running. Such circumstances require conditional loops.
There are two kinds of conditional loops: pre-test and post-test loops.
The statements in pre-test loops may or may not be executed at all, depending on the original values of loop-related variables. Post-test loops are always executed at least once, and the values of loop-related variables are tested at the end of the loop. The syntax is different:
pre-test loop:
while ({logical expression}) {
{statements that result in changing the value of the pre-test logical expression}
}
post-test loop:
do {
{statements that result in changing the value of the post-test logical expression}
} while ({logical expression});
Conditional loops can always be written either as post- or pre-test loops.
The choice is based on how a problem is stated. Consider this problem:
A small elevator has a maximum capacity of 500 pounds. People waiting in line to enter the elevator are weighed. If they can get on the elevator without exceeding the load limit, they are allowed to enter. If not, the elevator leaves without trying to find someone who weighs less than the person currentlyfirst in line. If the elevator is overloaded, it crashes. It is possible that there might be a large gorilla in line, weighing more than 500 pounds. This gorilla shouldn’t be allowed on the elevator under any circumstances. Write a document that will supply random weights for people (or gorillas) waiting in line, control access to the elevator, and stop allowing people (or gorillas) to enter if the weight limit would be exceeded.
One solution to this problem is shown in Document 5.6.
Document 5.6 (elevator.php)
<?php
echo"The elevator problem…<br />";
$limit=500;
echo"maximum weight = ".$limit." pounds<br />";
$totalWeight=0;$maxWeight=550;
do{
$newWeight=rand(0,$maxWeight);
if(($totalWeight+$newWeight) <=$limit) {
$totalWeight+=$newWeight;
echo"New weight = ".$newWeight.",
Total weight = ".$totalWeight."<br />";
$newWeight=0;
}
else echo"You weigh ".$newWeight." pounds.
I’m sorry, but you can’t get on.<br />";
}while(($totalWeight+$newWeight) <=$limit);
?>
5.5 Loop Structures 103
This code uses PHP’s rand() function (more about PHP math functions later) to gener- ate random weights between 0 and 550 pounds. The calcula- tions are done inside a post-test
loop. The code is arranged so that the effect of adding a new person to the elevator is tested before the person is allowed on the elevator.
In principle, count-controlled loops can also be written as conditional loops. However, it is better programming style to reserve conditional loop structures for problems that actually need them. Clearly, Document 5.6 is such a problem because there is no way for the script to determine ahead of time what weights the rand()function will generate.