PHP Switch Statement

Like other IF statements, the switch statement do the same job, i.e, comparing the same variable or expression with other different values and executing a different set of code depending on which value is equal to it. This is exactly the purpose, the switch statement is used for.

You can manage it by ‘if-elseif’ structure but it is tiresome. Another way to express your code here is using switch method, in which you can include several cases. First write the variable you want to manage its values in the parenthesis; then express each value after ‘case’; then write your desired code for this value and end with ‘break’; the other cases come in the same format. Look at the first example:

Syntax for PHP switch Statement:

Switch(var)
{
   case label 1: code to be executed if var=label1;
                 break;

   case label 2: code to be executed if var=label2;
                 break;

   default: code to be executed if var is different from both label1 and label2;
}

The following two examples are two different ways to write the same thing, one using a series of if and elseif statements, and the other using the switch statement:

<?php 
$Val=3;
switch ($Val){
          case 1:
                   echo 'First';
                   break;
          case 2:
                   echo 'Second';
                   break;
          case 3:
                   echo 'Third';
                   break;
          case 4:
                   echo 'Fourth';
                   break;
};
?>

‘Third’ would be printed.

It is allowed to use strings as cases:

<?php $food='cake';
switch ($food){
          case 'cake':
                   echo 'First';  // This will be ran
                   break;
          case 'pizza':
                   echo 'Second';
                   break;
          case 'biscuits':
                   echo 'Third';
                   break;
};
?>

But how does it work? At first, switch expression or variable will be evaluated. Then php starts from the first line; check its case with main evaluated expression; if they match with each other, the next codes after it will be ran, until it faces to the ‘break’; if you have forgotten break code, php will run all codes of other cases that comes after matched case; so don’t forget it. We have shown it in an example:

<?php 
$i=6;
switch ($i%5){  //=1
          case 1:  //This is the case
          case 3:
                   echo 'odd number';
                   break;
          case 2:
          case 4:
                   echo 'even number';
};
?>

Result:

odd number

You can also specify a case that is ran when all cases are false; its name is ‘default’:

<?php 
$m='c7';
switch ($m){
          case 'c1':
                   echo 'Methan';
                   break;
          case 'c2':
                   echo 'Ethan';
                   break;
          case 'c3':
                   echo 'Propane';
                   break;
          case 'c4':
                   echo 'Butan';
                   break;
          default:         // This is the case
                   echo 'C5+';  
                   break;
};
?>

Switch method basically has two alternative forms. The first is with colon and ‘endswitch’ and the second is with semicolon instead of colon. We have rewritten one of the above examples in two different styles below:

<?php 
$i=6;
switch ($i%5):
          case 1:
          case 3:
                   echo 'odd number';
                   break;
          case 2:
          case 4:
                   echo 'even number';
endswitch;
?>
<?php 
$i=6;
switch ($i%5){
          case 1;
          case 3;
                   echo 'odd number';
                   break;
          case 2;
          case 4;
                   echo 'even number';
};
?>

The switch statement is used to perform different actions based on different conditions.

Add a Comment

Your email address will not be published. Required fields are marked *

ABOUT CODINGACE

My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote