PHP switch case statement


Home - Tutorials - Basics

In this tutorial I will show you with code eaxmples how to use the switch - case statement in PHP.

Tutorial info:


Name:PHP switch case statement
Total steps:1
Category:Basics
Level:Beginner

Bookmark PHP switch case statement



Bookmark and Share

Step 1 - Switch case basics


PHP switch case statement

The switch case statement is a special format of conditional statements. It can be very usefull and makes your code more readable if you need to make a lot of decisions with the if - elseif - else statement. Besides this a switch case code snippet can be better to debug, maintain and it can result a bit faster code. However it has one disadvantage, that programmer usually forgot about the break statements at the end of each instuction block.

The general code flow can be seen on the flow chart below.

Switch case flowchart 

 

Let's suppose the following example where we use the if-else sollution:

Code example:
  1. $x = 3;
  2.  
  3. if ($x == 0) {
  4.     echo "0";
  5. } elseif ($x == 1) {
  6.     echo "1";
  7. } elseif ($x == 2) {
  8.     echo "2";
  9. } elseif ($x == 3) {
  10.     echo "3";
  11. } elseif ($x == 4) {
  12.     echo "4";
  13. } else {
  14.     echo "Not recognized";
  15. }
PHP F1
In this code we compare the same variable with many different values using the if statement. However this is the case where we could use the switch - case control strucure as well like this:
Code example:
  1. $x = 3;
  2.  
  3. switch ($x) {
  4.     case 0:
  5.         echo "0";
  6.         break;
  7.     case 1:
  8.         echo "1";
  9.         break;
  10.     case 2:
  11.         echo "2";
  12.         break;
  13.     case 3:
  14.         echo "3";
  15.         break;
  16.     case 4:
  17.         echo "4";
  18.         break;
  19. }
PHP F1
Although the switch sollution doesn't result a shorter code but it is a bit easier to understand. However you have to take care of the break statements which works as a code block terminator.
This example is not exactly the same as the "if" version as for example in case of $x=10 this code prints nothing. To solve this we can extend our switch statement.

Extend swich statement with default


To solve the previous problem we can extend our code with a default part. The code in this block works as the else block in case of the if satetement. So the complete switch statement will look like this:
Code example:
  1. $x = 13;
  2.  
  3. switch ($x) {
  4.     case 0:
  5.         echo "0";
  6.         break;
  7.     case 1:
  8.         echo "1";
  9.         break;
  10.     case 2:
  11.         echo "2";
  12.         break;
  13.     case 3:
  14.         echo "3";
  15.         break;
  16.     case 4:
  17.         echo "4";
  18.         break;
  19.     default:
  20.         echo "Not recognized";
  21.         break;
  22. }
PHP F1

The default case matches anything that wasn't matched by the other cases, and should be the last case statement. If you want more information about switch case statement you can visit the relevant wikipedia site and the official php manual pages.

 

 







Tags: PHP switch case, PHP switch, PHP case, switch case, php, switch, case

PHP switch case statement - Table of contents
Step 1 - Switch case basics


Follow phpf1 on Twitter




F1 Site Family
AJAX F1
CSS F1
Database F1
Flash F1
HTML F1
Java F1
JavaScript F1
PhotoShop F1
PHP F1
Scripts F1
Tutorial F1
Windows F1

Total time: 0.0307