Maximize your Laravel blog's potential with SEO best practices and reach 10K monthly clicks on Google.
Preview the course for free
Benjamin Crozat New!
Benjamin Crozat The art of crafting web applications

Switch in PHP: learn all about it

Benjamin Crozat — Updated on
Artboard

Hundreds of developers subscribed to my newsletter.
Join them and enjoy free content about the art of crafting websites!

Powered by

Table of contents

Switch in PHP: learn all about it

What is a switch statement in PHP?

The switch statement is similar to a if statement, but it is less repetitive and less prone to errors.

It should be used when you have to check a decent amounf of values. Let me explain.

Why use switch instead of if in PHP?

Sometimes, you may have to compare a variable or an expression to a significant amount of possible values. Doing it with an if statement can be repetitive and harder to read.

In the following example, a $language variable has been declared, and we display a thank you depending on its value. Here’s how it looks with an if:

$language = 'Français';
 
if ($language === 'English') {
echo 'Thank you!';
} elseif ($language === 'Español') {
echo '¡Gracias!';
} elseif ($language === 'Français') {
echo 'Merci !';
} elseif ($language === 'Italiano') {
echo 'Grazie!';
} else {
echo '🤷‍♂️';
}

Let’s now see how it looks with a switch.

How does the switch statement works in PHP?

Apart from the break statement, switch’s syntax is less repetitive and easier to read.

$language = 'Français';
 
switch ($language) {
case 'English':
echo 'Thank you!';
break;
case 'Español':
echo '¡Gracias!';
break;
case 'Français':
echo 'Merci !';
break;
case 'Italiano':
echo 'Grazie!';
break;
default:
echo '🤷‍♂️';
}
  1. Each case is run in the order it’s been declared;
  2. If the condition is met (in this example, $language equals “Français”), we run the code following the case statement;
  3. If the break statement isn’t used, each case will be executed after a match. Don’t forget to use it!
  4. The default case comes last and it has to be unique to avoid a fatal error.
Recommended

Debugging requires dissecting everything. Here's a list of all the one-line of code built-in ways to print arrays in PHP (and even Laravel-specific helpers).

There are multiple ways to check if an array is empty. Let me tell you about each of them and why and when you should use them.

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Store and manage files on Dropbox and use it to back up your Laravel app automatically. Compatible with PHP 8.1+ and Laravel 9+.

Take your code to the next level, thanks to exceptions. Handle errors in a more graceful way within try and catch blocks.

Start leveraging the power of AI today. It enables developers to do incredible things, and many startups build products around it.

Laravel 10 has been released on February 14, 2023. Let's dive into every relevant new feature and change.

Redirects in PHP are simple. I will guide you step by step and show you how to dodge some traps. Finally, we'll learn the nuance between 301 and 302 redirects.

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

Migrations are essential in any Laravel app using a database. I will tell you what they are, why you should use them and how you can generate them.

Powered by