PHP Switch Case Statement

Syntax of using the Switch case

The switch case is among the decision-making statements in PHP.

Before explaining the purpose and difference between the other decision-making statement (if..else) in the later part, let us first look at the syntax and live examples of using this.

Following is the general way to use PHP switch case statement:

Switch (expression){

case 1:

//Code to execute here

break;

case 2:

//Code for case 2

break;

default:

//The default case code here

//This will execute if none of the case is true

}

The flow chart of Switch case statement

 

PHP Swtich Case

An example of switch case statement

This is a basic example of using the PHP switch with 5 cases and a default case. We simply declared a variable and assigned it a value. The variable is used as an expression in the switch statement.

After that, five cases are created for the constant values of 1 to 5. Whichever case evaluates as true, in that case, its 5, the variable value will be displayed by using the echo statement. See the example and code:

$s = 5;
switch ($s) {
            case 1:
                echo "The executed case number is: 1";
                break;
            case 2:
                echo "The executed case number is: 2";
                break;
            case 3:
                echo "The executed case number is: 3";
                break;
            case 4:
                echo "The executed case number is: 4";
                break;
            case 5:
                echo "The executed case number is: 5";
                break;
            default:
                echo "The default case";
                break;
}

Output:

The executed case number is: 5

An example of switch case in PHP with Months

In the following example, we have used the date function to get the number of the current month. The month number can be taken by using the “n” date formatting character in the PHP date function.

The current month is assigned to a variable that acts as the expression in switch case PHP statement. Then we created twelve cases and whichever is true, it will display the month name.

In real programming, this may be quite useful in some scenarios where you may get the current month and perform some action on that basis.

Also, note that we did not create the default case as there are only twelve possibilities that are also the number of cases used.

See the code and output below:

$month = date("n");
switch ($month) {
            case 1:
                echo "The is Month of January!";
                break;
            case 2:
                echo "The is Month of February!";
                break;
            case 3:
                echo "The is Month of March!";
                break;
            case 4:
                echo "The is Month of April!";
                break;
            case 5:
                echo "The is Month of May!";
                break;
            case 6:
                echo "The is Month of June!";
                break;
            case 7:
                echo "The is Month of July!";
                break;
            case 8:
                echo "The is Month of August!";
                break;
            case 9:
                echo "The is Month of September!";
                break;
            case 10:
                echo "The is Month of October!";
                break;
            case 11:
                echo "The is Month of November!";
                break;
            case 12:
                echo "The is Month of December!";
                break;                                                                                                
}

Output:

The is Month of September!

What is the switch case statement?

The switch case is the decision-making statement in PHP. The if statement is also a decision-making statement in PHP and other programming languages. As a programmer, you should know the difference between the switch and if..else statements that where to use each depending on different scenarios.

Where should you use the switch case statement?

As such, the switch case and if else statements are the decision-making statements in PHP:

  • You should use the switch statement if there are many comparisons.
  • The switch is comparable to many nested if statements (else if) in PHP. So it makes compact or easily readable while using switch over many else if statements.
  • The switch uses primitive data types while cases use constant values. Whereas, in the if statement you can use ranges in expression. That results in the fast execution of switch statement where the compiler can use jump table to optimize it, generally.

How to use the PHP switch statement?

You have to use the switch keyword while using the switch case statement in PHP.

e.g. switch

  • An expression is given in the small brackets followed by an open curly bracket

e.g. switch ($month){

  • After that, cases are given with constant values.
  • After the constant value, statements to be executed are given there. Multiple statements are enclosed in curly braces.
  • Each case should end with a break statement. If you do not use the break statement, the next cases will keep on evaluating.
  • Optionally, you can use the default case in switch statement of PHP.
  • If none of the cases is true, the default case will be executed. This is comparable to the else statement of if…else decision-making statement. Where the else statement executes if none of the if or else if statements are true.
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!