Switch Statement
In addition to the if statements discussed in the previous section, PHP includes a fourth type of conditional statement called the switch statement. The switch statement is very similar or an alternative to the if...else if...else commands. The switch statement will test a condition. The outcome of that test will decide which case to execute. Switch is normally used when you are finding an exact (equal) result instead of a greater or less than result. When testing a range of values, the if statement should be used.
switch - use this statement to select one of many blocks of code to be executed.
Following is the correct switch Statement Syntax:
<?php
switch (expression) {
case "value1":
code to be executed if expression = value1;
break;
case "value2":
code to be executed if expression = value2;
break;
default:
code to be executed if expression is not equal to value1 or value2
}
?>
Like the if Statement, lines of code within the switch Statement are enclosed within curly braces. The braces define the beginning and end of the switch Statement. The following example demonstrates the use of the switch statement:
<?php
$number = 25;
switch ($number) {
case 40:
echo "The value of \$number is 40";
break;
case 25:
echo "The value of \$number is 25";
break;
default:
echo "The value of \$number is not 25 or 40";
}
?>
A switch statement can include many cases. In the previous example, two cases are shown. A variable $number is created and assigned the value 25. The switch statement is used to compare the value of $number against other values. The expression to be tested or compared (in this case $number) is placed inside of parenthesis immediately following the switch statement. Next a series of case statements are called to compare the expression to other values. These values are placed immediately after the case statement (NOTE: if the values being tested are strings, they should be enclosed in quotation marks). The value being compared to the expression is followed by a colon (:). Case statements are similar to the if and elseif constructs. If a case statement is true, any code associated with the statement is executed and the break statement is executed. The break statement forces the switch statement to terminate. No remaining cases will be evaluated. Finally, the switch statement includes a default case. This is similar to the else statement. If none of the case statements evaluate to TRUE, the default statement executed. There are a few things happening at the same time here :
- The switch condition is examined and a value is found ($number == 25).
- The condition value is passed through the cases.
- If the value matches a case value, the code in that block is executed. The break statement forces the switch statement to terminate. No remaining cases are examined.
- If the value does not match any of the case values, the default section is executed.
- In the above example, since the expression, the value of $number, is equal to 25, the second case statement is executed and the text "The value of $number is 25" is displayed in the browser window.