PHP switch Statement

The PHP switch statement is used when we need to execute particular block of code based on multiple cases/labels (conditions). For example:

<?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 above PHP example, is shown in the snapshot given below:

php switch statement

That is, the code inside the () bracket of switch considered as a single expression, that will be evaluated once. Therefore $x evaluated and the value after evaluation will be 4, as the value of $x is 4. Now this 4 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/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 found.

We can use switch statement to replace if...elseif...else. For example, the above example can also be written using if...elseif..else statement:

<?php
   $x = 4;
   if($x==1)
      echo "The value of \$x is 1";
   elseif($x==2)
      echo "The value of \$x is 2";
   elseif($x==3)
      echo "The value of \$x is 3";
   elseif($x==4)
      echo "The value of \$x is 4";
   elseif($x==5)
      echo "The value of \$x is 5";
   elseif($x==6)
      echo "The value of \$x is 6";
   elseif($x==7)
      echo "The value of \$x is 7";
   elseif($x==8)
      echo "The value of \$x is 8";
   else
      echo "The value of \$x is unknown";
?>

You will get same output as the output produced by previous example.

PHP switch Statement Syntax

The syntax of switch statement in PHP, is:

switch(x)
{
   case label1:
      // block of code to execute, if x==label1
      break;
   case label2:
      // block of code to execute, if x==label2
      break;
   case label3:
      // block of code to execute, if x==label3
      break;
   .
   .
   .
   case labelN:
      // block of code to execute, if x==labelN
      break;
   default:
      // block of code to execute, if no match found
}

PHP switch Statement Example

<?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?";
   }
?>

Since it is Sunday here, when I'm writing this post, therefore the output should be:

Today is Sunday

PHP Online Test


« Previous Tutorial Next Tutorial »