O'Reilly logo

Core PHP Programming: Using PHP to Build Dynamic Web Sites by Leon Atkinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The switch Statement

An alternative to if-elseif-else structures is the switch statement, which works on the assumption that you compare a single expression to a set of possible values. Figure 3-3 demonstrates the structure of a switch statement.

Figure 3-3. The switch statement.

The root expression inside a switch statement is evaluated and then compared to each expression following a case statement. At the end of the list of cases you can put a default statement that works exactly like an else statement; it matches if no other case matches.

Notice that cases don't have curly braces after them. This reveals an important difference between ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required