Expand To Show Full Article
PHP Switch Case Statement With Examples - Itsourcecode.com

PHP Switch Case Statement With Examples

In PHP, switch case is a conditional statement which is used to execute one or more conditions.

In this article, we will talk about Switch Case statement in a detailed explanation as well as example programs that could be helpful to your learning on this powerful conditional statement. This topic is a continuation of the previous topic, entitled PHP in_array.

What is PHP switch case?

The Switch Case Statement is almost used for instances that we want to execute multiple conditions in our program.

Further, the switch case is similar to if-else-if statement. Yet, in some instances the switch case is used to compare the same (expression or variable) with multiple values. And execute different conditions based on what value they are equal to.

What is the syntax of switch case?

Syntax:

switch (x) {
  case labelOne:
    code to be executed if x=labelOne;
    break;
  case labelTwo:
    code to be executed if x=labelTwo;
    break;
  case labelThree:
    code to be executed if x=labelThree;
    break;
    ...
  default:
    //break default
    code to be executed if x is different from all labels;
}

How does switch work in PHP?

This is how switch case works – the first thing we do is declared a single expression x or most probably known as (variable), which is evaluated only once.

Further, the given value of the variable is being compared to the values for each case in the given structure. If the given value where matches, the associated block of code with that case will be (executed).

Lastly, the break case block is mainly used to prevent running the code automatically into the next case, then the (default) statement is executed when there is no match found in each case.

Example:

<?php
$programmer = "Glenn";

switch ($programmer) {
  case "Glenn":
    echo "You choose Glenn as your programmer!";
    break;
  case "Jude":
    echo "You choose Jude as your programmer!";
    break;
  case "Adones":
    echo "You choose Adones as your programmer!";
    break;
  default:
    echo "There is no programmer name neither Glenn, Jude, nor Adones!";
}
?>

Output:

You choose Glenn as your programmer!

Which is better if-else or switch PHP?

Both of them is better but there is always a difference between their functionalities, the if-else and elseif statement is better once the given value is boolean and it is really great for variable conditions which result into boolean values.

While a PHP switch statement is proven to be faster than if they are provided by a number of cases which proven to be good and also better for a data values which are fixed.

What is the alternative to PHP switch?

The alternative of switch case is match() function which was introduced in the 8th version of PHP.

Example of match() function

$status = match($request_method) {
    'post' => $this->handlePost(),
    'get', 'head' =>  $this->handleGet(),
    default => throw new \Exception('Unsupported'),
};

The above expression is a functionality of match() function, while compared to switch block below.

switch ($request_method) {
 $status = match($request_method) {
   case 'post':
       $status = $this->handlePost();
       break;
   'post' => $this->handlePost(),
   case 'get':
   case 'head':
       $status = $this->handleGet();
       break;
   'get', 'head' =>  $this->handleGet(),
   default:
       throw new \Exception('Unsupported'); 
   default => throw new \Exception('Unsupported'),
 }
 };

Does Switch Case improve readability?

The switch case statement can improve the readability of our code block and allow us to replace some nested if-else constructs.

Can switch case have duplicates?

The switch case can’t have any duplicates, if there is an instance that the (switch) statement is identical, the second case probably will be escaped at the time of execution and display a result of copy paste error where the first (case) is copied and not properly rename.

What are the disadvantages of switch case?

The drawbacks of switch case statement is the declaration of variables wherein they can only have a char and int data type. It means that the float or no data type is not allowed.

How is switch executed?

The switch case execution begins on the body at the first statement in or after the labelled-statement is matched.

Further, the switch case execution will still proceed until the body will end, or else until a break statement transfers all the control out of the body.

Summary

In summary, you have learned about PHP Switch Case. This article also discussed switch case syntax, alternative of PHP switch, does switch case improve readability, can switch case have duplicates, disadvantages of switch case, and how switch case executed.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment