PHP Tutorial switch statement

Register here to save your progress

? ? ?

Overview

The switch statement runs a specific block of code from multiple ones, based on a single expression (that usually is a variable). It works quite similar with multiple 'if' statements, stacked together, checking a single expression.

<?php

//set pressed button
$button_colour = 'red';
$flavour = '';

//pick the flavour
switch ($button_colour)
{
   case 'brown':
      $flavour = 'chocolate';
      break;

   case 'pink':
      $flavour = 'strawberry';
      break;

   case 'red':
      $flavour = 'cherry';
      break;

   case 'yellow':
      $flavour = 'mango';
      break;
   
   default:
      $flavour = 'vanilla';
}

//show picked flavour
echo 'You chose '.$flavour.' ice-cream.';

/*
will output 'You chose cherry ice-cream', since our button colour was red
this expression would be similar with the one bellow
*/

if($button_colour == 'brown')
{
      $flavour = 'chocolate';
}
elseif($button_colour == 'pink')
{
      $flavour = 'strawberry';
}
elseif($button_colour == 'red')
{
      $flavour = 'cherry';
}
elseif($button_colour == 'yellow')
{
      $flavour = 'mango';
}
else
{
      $flavour = 'vanilla';
}

//show picked flavour
echo 'You chose '.$flavour.' ice-cream.';

As visible from the example above, when dealing with many similar repetitive expressions, switch can give a cleaner alternative to the if statement.

Can a switch statement be converted to an if statement?

break statement

Switch statement work by checking our expression against the value of the each case.

So, it starts by checking the value of our first case, if it matches the value than runs it's block of code, than it moves to the second case and checks it's value, it's maches again than it runs also it's code and than moves to the third case and so on. 

It we want to exit the switch statement once it maches a case, we need to use the break statement. Break statement instructs the execution to exit the switch structure and move forward.

<?php

$day_number = 6;

switch($day_number)
{
   case 1:
   case 2:
   case 3:
   case 4:
   case 5:
      echo 'Today is a working day.';
      break;

   case 6:
   case 7: 
      echo 'Today is weekend.';
      break;

   default:
      echo 'Today is an interesting day';
}

/*

it will output 'Today is weekend'
all the cases that have the value between 1 and 5 will output 'Today is a working day' since they don't have a break statement, same with those that have the value between 6 and 7 will output 'Today is a working day'

*/

What will be the output of the following script?

<?php

switch(!true)
{
    case !!0:
        echo 'A';
        
    case 1 == '1':
        echo 'B';

    case false || !false:
        echo 'C';
        break;
      
    case true && !false:
        echo 'D';
        break;
      
    case true !== 1:
        echo 'E';
      
}

default statement

After evaluating the values of all the cases an switch statement may not mach any value, if we want to have block to be ran if there's nothing matched we use the default statement, as the default case. Comparing to the if ... elseif ... else conditional structure , it's the equivalent of the else statement.

<?php

$animal = '';

switch($animal)
{
   case 'dogs':
      echo 'I love dogs.';
         break;
   case 'cats':
      echo 'I love cats.';
         break;
   case 'fish':
      echo 'I love fish.';
         break;

   default:
      echo 'I love all the animals.';
}


/*

will run the default statement as none of the above ones had values to match the expression

*/

When will 'default' statement be ran?