|In a previous tutorial we discussed how to use the PHP if statement in your programs to ask questions and make decisions. By using the if statement you can control the flow of your program. Now we will explore the PHP switch statement which also can be used to control the flow of your program.|
The PHP if statement evaluates an expression as true or false. This type of question is called a boolean. Is the sky blue - True or false? However, the switch statement evaluates a case result against a switch expression. This is a fancy way of saying that you can test for more then just true or false. You can also test for simple numbers and strings. Here is the basic switch statement.
As you can see from the example the switch statement is made up of six parts.
The switch statement begins with the expression to be tested - $sky - placed inside the parenthesis.
The case statement tests the value of the switch expression - $sky - against the result which in the example is red. If there is a match, the program will complete the accompanying code for that case statement. echo "The sky is red";