PHP switch case statement with 2 demos online

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:

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 by clicking the link or image below:

PHP_swtich_case_demo_1

 

See online demo and code

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 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 the 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 example and code by clicking the link or demo image below:

PHP-swtich-with-month

See online demo and code

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 compares.
  • 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 up by a break statement. If you do not use the break statement, 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.