Page 62 - Open Soource Technologies 304.indd
P. 62
Web Technologies-I
Notes
switch ($xyz) {
// each of these three cases
// will process the same statements
case $xyz < 0:
case 0:
case 10:
echo ($xyz . ‘ is not a value!’);
break;
default:
echo ($xyz . ‘ is ready for processing!’);
$abc = someFunc($xyz);
break;
}
Here is a bad example of omitting break statements. The code will fall through and create a bit
of a mess.
// A poorly formed switch statement
switch ($xyz) {
case is_int($xyz) == false:
echo ($xyz . ‘ is not an integer!’);
case $xyz <= 0:
echo ($xyz . ‘ is too low!’);
case $xyz >= 10:
echo ($xyz .+ ‘ is too high!’);
default:
echo ($ xyz . ‘ is within acceptable limits!’);
$abc = someFunc($xyz);
break;
}
If we assume for right now that $xyz is not an integer:
• The code will report that $xyz is not an integer.
• Then report that it is too low a number.
• Then report it is too high.
• Then it reports that it is an acceptable value.
Then it will submit it to our function, which we can surmise from the code was expecting a
numeric value between 1 and 9, not something else. This, needless to say, would most probably
generate an error.
Unless your goal is to confuse the user, what you would have is what is called a big mess.
56 LOVELY PROFESSIONAL UNIVERSITY