PHP switch statement

The PHP "switch" statement is a control structure that executes distinct blocks of code based on the value of a single expression.

PHP switch statement syntax

The syntax, or the general form, of the PHP "switch" statement is:

switch (expression) {
  case value1:
    // block of code to execute, if "expression" returns "value1"
    break;
  case value2:
    // block of code to execute, if "expression" returns "value2"
    break;
  case value3:
    // block of code to execute, if "expression" returns "value3"
    break;
  .
  .
  .
  case valueN:
    // block of code to execute, if "expression" returns "valueN"
    break;
  default:
    // block of code to execute if "expression" returns a value
    // that does not match with any cases
    break;
}

The "switch (expression)" is the start of the switch statement, which evaluates the expression to determine which code block should be executed. Expressions can be any type of data that can be compared, such as variables or function calls.

The "break;" statement is used to end each case. It instructs PHP to exit the switch statement and proceed to the next line of code after executing the case-specific code block. If you omit this statement, the switch statement will continue to execute the code blocks of all subsequent cases, even if they do not match the expression.

The switch statement's default case. If none of the specified cases match the expression, this code block is executed. It is not necessary to include a default case in a switch statement, but it is generally recommended for handling unexpected values.

PHP switch statement example

Consider the following PHP code as an example of the "switch" statement:

<?php
   $x = 4;
   switch($x)
   {
      case 1:
         echo "The value of \$x is 1";
         break;
      case 2:
         echo "The value of \$x is 2";
         break;
      case 3:
         echo "The value of \$x is 3";
         break;
      case 4:
         echo "The value of \$x is 4";
         break;
      case 5:
         echo "The value of \$x is 5";
         break;
      case 6:
         echo "The value of \$x is 6";
         break;
      case 7:
         echo "The value of \$x is 7";
         break;
      case 8:
         echo "The value of \$x is 8";
         break;
      default:
         echo "The value of \$x is unknown";
   }
?>

The output produced by the above PHP example is shown in the snapshot given below:

php switch statement

That is, the code inside the () bracket of a "switch" is considered a single expression and will be evaluated once. Therefore, $x is evaluated, and the value after evaluation will be 4, as the value of $x is 4. Now this four gets compared with each and every case in the structure. Since 4 matches with case 4, therefore, the block of code associated with this case will be executed.

Note: The "break" keyword or statement is used to skip the execution of remaining cases.

Note: The "default" case is used to execute some block of code if no match is found.

We can use a "switch" statement to replace if...elseif...else.

Here is another example demonstrating the "switch" statement in PHP.

<?php
   $day = date("D");
   switch ($day)
   {
      case "Mon":
         echo "Today is Monday";
         break;
      case "Tue":
         echo "Today is Tuesday";
         break;
      case "Wed":
         echo "Today is Wednesday";
         break;
      case "Thu":
         echo "Today is Thursday";
         break;
      case "Fri":
         echo "Today is Friday";
         break;
      case "Sat":
         echo "Today is Saturday";
         break;
      case "Sun":
         echo "Today is Sunday";
         break;
      default:
         echo "What! are you an alien?";
   }
?>

Whatever the current day is when executing this PHP code, it will be printed on the output.

Advantages of the switch statement in PHP

Disadvantages of the switch statement in PHP

PHP Online Test


« Previous Tutorial Next Tutorial »


Liked this article? Share it!





Like/follow us on social media for updates!




Like/follow us on social media for updates!