The PHP switch statement

PHP Fundamentals

Published: May 15, 2021 by C.S. Rhymes

PHP Fundamentals

This post is part of a series of posts about the fundamentals of PHP.

In the previous article we discussed the if statement, where it said you can have many different elseif statements if you wanted to handle many different scenarios, but it gets to a point where you should consider swapping to a switch statement.

$myVar = 'green';

if ($myVar === 'red') {
    echo 'It is red';
} elseif ($myVar === 'blue') {
    echo 'It is blue';
} elseif ($myVar === 'green') {
    echo 'It is green';
}

This can be rewritten using a switch statement. Each condition you want to match has a case where you pass in the variable you want to match. Within the case, you put the code you want to run if the condition matches. Then you need to add a break, otherwise the code will continue to check for matches in the rest of the switch statement.

$myVar = 'green';

switch ($myVar) {
    case 'red':
        echo 'It is red';
        break;
    case 'blue':
        echo 'It is blue';
        break;
    case 'green':
        echo 'It is green';
        break;
}

Default case

A very useful feature of the switch statement is allowing a default if none of the other cases match. Sometimes you don’t know what the variable will be and it allows you to catch this edge case. You could even use it to throw an exception to deliberately stop any further code running.

$myVar = 'orange';

switch ($myVar) {
    case 'red':
        echo 'It is red';
        break;
    case 'blue':
        echo 'It is blue';
        break;
    case 'green':
        echo 'It is green';
        break;
    default:
        throw new Exception('It is not a matching colour');
}

// Fatal error: Uncaught Exception: It is not a matching colour

Multiple case matching

Sometimes you want to do the same thing for multiple matching cases. If you were to use an if statement you would need to either repeat the code multiple times or use an or (||) in your condition.

$myVar = 'green';

if ($myVar === 'red' || $myVar === 'green') {
    echo 'It is red or green';
}

In a switch statement you can do this easily by listing multiple cases one after the other, then adding your code to run with a break after;

$myVar = 'green';

switch ($myVar) {
    case 'red':
    case 'green':
        echo 'It is red or green';
        break;
    case 'blue':
        echo 'It is blue';
        break;
}

Returning from a switch case

Sometimes you don’t need a break in a switch statement. This is when you directly return from the switch statement. The example below has a switch statement in a function, returning the result from the matching case.

function findTheColour($colour)
{
    switch ($colour) {
        case 'red':
            return 'It is red';
        case 'blue':
            return 'It is blue';
        case 'green':
            return 'It is green';
        default:
            return 'It does not match';
    }
}

echo findTheColour('green'); // It is green

I know that some developers (such as me) think it looks strange not having the breaks in a switch statement as it’s nice to break up the code.

Alternative syntax

As with an if statement, you can also use colons instead of brackets and end the switch with endswitch.

switch ($myVar):
    case 'red':
        echo 'It is red';
        break;
    case 'blue':
        echo 'It is blue';
        break;
    case 'green':
        echo 'It is green';
        break;
endswitch;

You can also use semicolons instead of colons after the case if you wanted to.

case 'red';

Using an Array instead

Some people don’t like using switch statements as they can seem a bit verbose. There is a potential alternative using an array to provide the options if it is a simple scenario.

$colours = [
    'red' => 'It is red',
    'green' => 'It is green',
    'blue' => 'It is blue',
];

$myVar = 'green';

echo $colours[$myVar]; //It is green

The above will work fine for red, green or blue, but if it is an unknown colour, such as orange, then you will end up with an undefined index error.

You could use a null coalescing operator (PHP 7.0 onwards) to catch this error and return a default response.

echo $colours[$myVar] ?? 'It does not match';

Match

PHP 8.0 has introduced the match statement. It offers shorter syntax and it returns a value. There is a great article about the differences between match and switch on stitcher.io by Brent.

PHP Code Beginner

Share

Latest Posts

Mocking window.location in Jest
Mocking window.location in Jest

Recently I had to write some tests for a piece of JavaScript code that used window.location. This left me trying to figure out how to mock the window.location so that I could pass in dummy data and ensure that the data I got back was what was expected. Here was how I managed to solve the issue.

Considerations for Incremental Static Regeneration in Next.js
Considerations for Incremental Static Regeneration in Next.js

Next.js offers a feature called Incremental Static Regeneration (ISR) that allows you to generate a static page when the page is first visited, rather than generating a static copy at build time. This is a really handy feature as it allows you to reduce your build time, but still benefit from having a cache of a page generated so future visitors will have a faster response time.

Mocking axios in Jest tests with Typescript
Mocking axios in Jest tests with Typescript

Recently I wanted to write a test for a React component that uses axios to retrieve information from an API. To do this I had to mock jest, but I ran into some issues with the types as I was using typescript. This article explains how I managed to get it to work.

How NOT to make a website

How NOT to make a Website

By C.S. Rhymes

From £8.99

Nigel's Intranet Adventure

Nigel's Intranet Adventure

By C.S. Rhymes

From £2.69