Switching

Having multiple if statements in one place is ugly, slow, and prone to errors. Consider the code in Listing 29.3.

Listing 29.3. How Multiple Conditional Statements Lead to Ugly Code

if ($cat_age == 4) { echo "Cat age is 4"; } else {

echo "Cat age is unknown";

Even though it certainly works, it is a poor solution to the problem. Much better is a switch/case block, which transforms the previous code into what's shown in Listing 29.4.

Listing 29.4. Using a switch/ case Block

<?php

$cat age = 3;

switch ($cat

age)

{

case 1:

echo "Cat

age

is

1";

break;

case 2:

echo "Cat

age

is

2";

break;

case 3:

echo "Cat

age

is

3";

break;

case 4:

echo "Cat

age

is

4";

break;

default:

echo "Cat

age

is

unknown";

}

?>

Although it is only slightly shorter, it is a great deal more readable and much easier to maintain. A switch/case group is made up of a switch() statement in which you provide the variable you want to check, followed by numerous case statements. Notice the break statement at the end of each case. Without that, PHP would execute each case statement beneath the one it matches. Calling break causes PHP to exit the switch/case. Notice also that there is a default case at the end that catches everything that has no matching case.

It is important that you do not use case default: but merely default:. Also, it is the last case label, so it has no need for a break statement because PHP exits the switch/case block there anyway.

Was this article helpful?

0 0

Post a comment