Table of Contents



Introduction

The switch statement is similar to a series of if statements on the same expression. This is applicable to the case where we may want to test the same expression with many different values, and execute a different block of statements depending on which value it is equal to.

Why the switch Statement?

Imagine we have a variable $x which we would like to test for different values, executing a different statement depending on its value. This can be achieved using the if…elseif…else construct which we have discussed.

1if ($x == 0) {
2    echo "x equals 0";
3} elseif ($x == 1) {
4    echo "x equals 1";
5} elseif ($x == 2) {
6    echo "x equals 2";
7}

The above if...elseif...else statement is equivalent to the following switch statement. Each possible value of the variable $x is placed after the case keyword in each block. If there is a match in value, the corresponding block of statements is executed. Thereafter, a break keyword at the end of the block tells PHP to exit the switch statement.

We immediately see that the switch statement is less cluttered and yet achieves the same results as the if...elseif...else statement.

 1switch ($x) {
 2    case 0:
 3        echo "x equals 0";
 4        break;
 5    case 1:
 6        echo "x equals 1";
 7        break;
 8    case 2:
 9        echo "x equals 2";
10        break;
11}

ADVERTISEMENT



Syntax of the switch Statement

The syntax of the switch statement is given as:

Syntax

The switch statement.

 1switch (expr) {
 2    case case1:
 3        statements;
 4        break;
 5    case case2:
 6        statements;
 7        break;
 8    case case3:
 9        statements;
10        break;
11    ...
12    default:
13        statements;
14}

The expression expr is evaluated at the beginning of the structure. It will return a value (numeric, string, or Boolean) and case1, case2, case3, etc. are the possible values considered for expr. PHP examines each case value to see whether one matches the result expr and, if it does, executes the corresponding block. The break keyword tells PHP to stop processing the rest of the switch statement.

In case none of the cases matches the value of expr, the statements in the default block are executed. Note that there is no need for a break keyword for the default block since it is already at the end of the switch statement. However, the default block is optional and may be omitted.

There is also an alternative syntax of the switch statement where we may replace the first curly brace with a single colon and the final curly brace with an endswitch keyword.

Syntax

The switch statement (alternative syntax).

 1switch (expr):
 2    case case1:
 3        statements;
 4        break;
 5    case case2:
 6        statements;
 7        break;
 8    case case3:
 9        statements;
10        break;
11    ...
12    default:
13        statements;
14endswitch;

Using the switch Statement

Example

Using the switch statement.

 1<?php
 2    $season="summer";
 3    switch($season) {
 4        case 'spring':
 5            $footwear = 'track shoes';
 6            break;
 7        case 'summer':
 8            $footwear = 'slippers';
 9            break;
10        case 'fall':
11            $footwear = 'hiking boots';
12            break;
13        case 'winter':
14            $footwear = 'snow boots';
15            break;
16    }
17    echo "The current season is $season. Please wear $footwear.";
18?>
The current season is summer. Please wear slippers.

It is also possible to use the switch statement when each possible value of expr falls within ranges.

Example

Using the switch statement for ranges of values.

 1<?php
 2    $salary = 8000;
 3    $KPI = 62;
 4    switch (true){
 5        case ($KPI > 80):
 6            $bonus = 1300;
 7            $salary += $bonus;
 8            break;
 9        case ($KPI > 70 && $KPI <= 80):
10            $bonus = 1000;
11            $salary += $bonus;
12            break;
13        case ($KPI > 60 && $KPI <= 70):
14            $bonus = 500;
15            $salary += $bonus;
16            break;
17        default:
18            $bonus = 0;
19            $salary += $bonus;
20    }
21    echo "Your bonus is $bonus. <br>" ;
22    echo "Your updated salary is $salary." ;
23?>
Your bonus is 500.
Your updated salary is 8500.

Compare the above example with a previous example using the if...elseif...else statement.

ADVERTISEMENT



As a final example, what if we have a switch statement where a certain outcome can result from more than one case?

Example

Using the switch statement where multiple cases result in same outcome.

 1<?php
 2    $month = "May";
 3    switch ($month){
 4        case "February":
 5            $daysInMonth = 28;
 6            echo "There are $daysInMonth days in the month of $month.";
 7            break;
 8        case "January":
 9        case "March":
10        case "May":
11        case "July":
12        case "August":
13        case "October":
14        case "December":
15            $daysInMonth = 31;
16            echo "There are $daysInMonth days in the month of $month.";
17            break;
18        case "April":
19        case "June":
20        case "September":
21        case "November":
22            $daysInMonth = 30;
23            echo "There are $daysInMonth days in the month of $month.";
24            break;
25        default:
26            echo "Please enter a valid month.";
27    }
28?>
There are 31 days in the month of May.

In the above example, everything after the first matching case will be executed until a break statement is found. This allows us to group the different cases corresponding to the same outcome. Courtesy of stackoverflow .