Benjamin Crozat The web developer life

Switch in PHP: learn all about it

Benjamin Crozat Benjamin Crozat2 minutes read

Before you start reading this article, did you know 76 persons subscribed to my newsletter?

Join them and enjoy free content about the art of crafting websites!

Switch in PHP: learn all about it
Table of contents

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.

Other posts to read

AI is a trending topic in the programming space. It enables developers to do incredible things, and many startups build products around it.

Job interviews are not as tough as they seem. I will give you guidance through my past experiences and a list of easy questions that will make you stand out.

Here's a case study for my blog in the programming niche, where I share everything I did to increase clicks by a huge amount since the beginning.

Debugging requires dissecting everything. Here's a list of all the one-line of code built-in ways to print arrays in PHP.

Whether PHP is dead or not is a matter of numbers, not opinions. I will provide you with essential statistics that will lead you to make better decisions.

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

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.

Learn why the "Invalid argument supplied for foreach()" warning happens, and let me show you multiple ways to fix it.

PHP 8.3 will be released in November 2023, and as usual, you need to be up to date with new features and breaking changes for easier transitions.

Learning a framework can be overwhelming. Time and practice will make you a master. Here are some best practices to help you toward your goal.