Use a PHP switch statement for advanced conditional logic

Right now, we are using a ternary to check if we have posts, and displaying an appropriate message. But what if we wanted something a bit more complex?

Ternaries are only really good for simple booleans with this or that logic, but not this, that & then “something else” logic. If statements are ok for these situations, but they can lead to some repeated logic throughout your code.

In this situation, it’s better to use a switch statement. With a switch, you supply a condition, then define any number of cases the condition may match.

Let’s create a switch. Just like an if statement we supply a condition, which will just be $numPosts. Then, we’ll have an open & close bracket, just like an if statement.

The following format is where things start to differ. Next comes a case keyword, followed by the result we wish to match resulting from the condition. Since we are passing in $numPosts, our matching result will be a number.

For example, if we wanted to target 0 posts, we will write out case 0. Next comes a colon.

Everything after this colon is considered a separate logic block, and all code continues to execute until it encounters a break keyword. Let’s create a new $message variable, and assign it the value: “There are no posts.”

Since we want to stop, we will type the break keyword, followed by a semicolon.

<?php
$title = 'My Blog';
$numPosts = 10;
$hasPosts = $numPosts > 0;
$numPostsDisplay = "\\"$numPosts\\" posts";
switch ($numPosts) {
    case 0:
        $message = 'There are no posts.';
        break;
}
?>
<h1><?= $title ?></h1>
<h2><?= $numPostsDisplay ?></h2>
<p><?= $message ?></p>

For any other condition matches, we will repeat the process.

Let’s set up case 3 for our logic to match 3 posts. We’ll assign the message “There are a few posts.”, then type our break; keyword to stop the logic from continuing.

One neat think we can do is that if we want this message to also display for a result matching not only 3, but 1 & 2 as well, we can define multiple case statements and have them all fall back to this single statement. So we can type case 1:, case 2: before case 3 and have them come right after each other. Since PHP doesn’t encounter a break; keyword, it will continue processing all of these matching conditions as a single block to execute.

<?php
$title = 'My Blog';
$numPosts = 10;
$hasPosts = $numPosts > 0;
$numPostsDisplay = "\\"$numPosts\\" posts";
switch ($numPosts) {
    case 0:
        $message = 'There are no posts.';
        break;
    case 1:
    case 2:
    case 3:
        $message = "There are a few posts.";
        break;
}
?>
<h1><?= $title ?></h1>
<h2><?= $numPostsDisplay ?></h2>
<p><?= $message ?></p>

Finally, we can also supply a default fallback to handle the event when we do not define a case statement that matches the condition. This will just be the default keyword followed by a colon. Since this will only execute when the result is 4 or more, we will just say “There are many posts.”. We do not need a break keyword here since this is the end of the switch statement.

<?php
$title = 'My Blog';
$numPosts = 10;
$hasPosts = $numPosts > 0;
$numPostsDisplay = "\\"$numPosts\\" posts";
switch ($numPosts) {
    case 0:
        $message = 'There are no posts.';
        break;
    case 1:
    case 2:
    case 3:
        $message = "There are a few posts.";
        break;
    default:
        $message = 'There are many posts.';
}
?>
<h1><?= $title ?></h1>
<h2><?= $numPostsDisplay ?></h2>
<p><?= $message ?></p>

Finally, let’s add this variable as a message outputted at the end within a <p> tag.

When we test out 0, 3, and 10, we will see the conditions match and execute the appropriate $message value.

Note that conditions on switch statements are loose type checks, meaning that it will also match strings of the same value.

Complete and Continue