PHP Switch Statement

The Switch Statement can go through many blocks of code. It is similar to an If Else Statement but is much easier to understand.

Syntax

<?php

switch (condition){

case 1:
code to be executed if condition = 1
break;

case 2:
code to be executed if condition = 2;
break;

default:
code to execute
} 

?>
Sponsored Links

Example

Make a new variable called num and give it the value 1.

<?php

$num = 1;

switch ($num){

case 1:
echo 'The number is 1!';
break;

case 2:
echo 'The number is 2';
break;

default:
echo 'Unknown number';
} 

?>

This code will look like this in an If Else Statement since they are so similar:

<?php

$num = 1;

if ($num == 1){
echo 'The number is 1';
}elseif ($num == 2){
echo 'The number is 2';
}else{
echo 'Unknown number';
}

?>

As you can see, the Switch Statement is clearer to read and easier to manage if you want to change the variable name. The break keyword in the Switch Statement prevents the Switch Statement from looping through rest of the code. If you remove the break keyword from the Switch Statement, you will see the rest of the results echo out. The default keyword is the default result given if nothing matches the condition, and it goes at the end of the case labels.

Example 2 - Strings

<?php

$name = "Asim";

switch ($name){

case "Asim":
echo "Welcome Asim";
break;

case "David":
echo "Welcome David";
break;

case "Mark":
echo "Welcome Mark";
break;

default:
echo "Unkown user!";

}

?>

Similar to the above Switch Statements, this evaluates a string instead of a integer. Try it below: enter 16, 17 or 18 and you will get a message. Enter a larger number and another message will appear, and if you enter something else, the default value kicks in.

Using Operators

You can use these operators, and they must be enclosed in brackets ( ):

Operator Description Example
< Less than ($age < 20)
> Greater than ($age > 20)
<= Less than or equal to ($age <= 20)
>= Greater than or equal to ($age >= 20)
!= Not equal to ($age != 20)
== Equal to ($age == 20)

Example

switch ($age){  

    case 16:
    echo 'You\'re not old enough to play GTA , but you can play GRID!';
    break;


    case 17:
    echo 'You\'re old enough to drive!';
    break;

    case 18:
    echo 'You\'re old enough to play GTA!';
    break;


    case ($age > 18):
    echo 'You\'re quite old!';
    break;    

    default:
    echo 'Was that a number?'; 

}

In the above example we use the greater than sign to see if $age is greater than 18. You can also use the equal to operator to compare values. Try the above example below.

Try it!


Case Sensitivity

At times you might collect user data and loop through the results, but the cases may differ which could affect the results. For example, take this:

$food = "Pasta";

switch ($food) {
case "pasta" :
            echo 'Pasta it is';
            break;
case "pizza" :
            echo 'Pizza it is!';
            break;

            default:
            echo "I don't know";
}

This will output “I don’t know”, because although pasta is one of the case labels it is in a different case. We can solve this issue by using the strtolower function. This will force the value in $food to be all lower case.

$food = "Pasta";

switch (strtolower($food)) {
case "pasta" :
            echo 'Pasta it is';
            break;
case "pizza" :
            echo 'Pizza it is!';
            break;

            default:
            echo "I don't know";
}

This will now output the correct result. This will only work if the case labels are lower case, so make sure when you type the labels they are lower case and not upper. Just remember when comparing something in different cases you’ll have to force the case to either upper or lower; lower is probably best as most people do not normally type in upper case.

Summary

This tutorial covered the Switch Statement, how to use operators as well as the keywords used. You also learned about case sensitivity issues. Just remember that if you are going to evaluate a single variable with multiple outcomes use a Switch Statement instead of an If Else Statement.