PHP Switch | How the Switch Statement Works in PHP

The PHP switch statement is multiple conditions to only execute one block if its condition generating a true boolean value.

The PHP switch can also be replaced of PHP Else if statement in the control flow. So when one condition is achieved, it will execute the block.

Anyway, to summarize that, the PHP switch contains unlimited cases which can set the value to comparing each case. So if it achieves the condition, it will execute the statements.

The PHP switch syntax would be like the following code.

switch($param) {
  case 'value':
  break;

  ...... multiple cases here
  default:
  // -- Default execution 
}

Basic Structure of PHP Switch Statement

The PHP switch statement is a predefined function, takes one parameter as a value, it is doing thing like a loop to check this value with a list of conditions.

<?php
  $number = 6;
  switch ( $number ) {
    case 1:
      echo "Number 1";
    break;

    case 2:
      echo "Number 2";
    break;
   
    case 3:
      echo "Number 3";
    break;
   
    default: 
     echo "Another Number";
  }
?>

In the below, you will see the components of the PHP switch statement.

The PHP switch is similar to if, else if, and else. It is doing the same job, but here it is using a predefined function and inside it doing conditions through a something called cases.

So switch ( $param ){ .. } can be written like this in if condition if ( $param ){ .. }. Its cases similar to else if ( $param ) { .. } condition.

Let’s compare if statement with switch statement by the following example.

The if statement shows you 5 cases.

<?php
  $value = 5;
  if ( $value == 1 ) {
    // ... Statements
  } else if ( $value == 2 ) {
    // ... Statements
  } else if ( $value == 3 ) {
    // ... Statements
  } else if ( $value == 5 ) {
    // ... Statements
  } else {
    // ... Statements
  } 
?>

And to do the same code in switch statement, follow the below code.

<?php
  $value = 5;
  switch ( $value ) {
    case 1:
      // ... Statements
    break;
    
    case 2:
      // ... Statements
    break;

    case 3:
      // ... Statements
    break;
    
    case 5: 
      // ... Statements
    break;
    
    default:
      // ... Statements
  } 
?>

Break with Switch Statement

The break in switch means to execute the current statements, then prevent the other pieces of code from the execution and exit the switch from the condition.

What will happen if there is a switch statement without a break statement?

<?php 
  $ct = 10;
  switch( $ct ) {

    case 1: // => will not execute this block 
      echo "Will not execute the line !";
      

    case 10: // => Execute this
      echo "Will execute the line ! \n";
      
                 
    case 3: // => Execute this
      echo "Will execute the line ! \n";
       

  }
?>

The condition will only start from the correct case, then execute the following cases. For more details, read the break tutorial.

Executing One Block for Two or More Switch Cases

To do that, follow the below code.

<?php 
  $ct = 10;
  switch( $ct ) {

    case 1:  
    case 10:  
    case 5:
      echo "Will execute this line !";
      break;
    
    case 11:
      echo "Will not execute this line ! \n";
      break;
    
  }
?>

Wrapping Up

The PHP switch seems to if condition but here it allows us to execute multiple conditions through cases, each one has a block of code.

The break statement to exit the switch condition at a specific point.

Similar Reads

Class and Object in PHP: How They Work with a Real Example

Classes and objects are important parts of OOP in PHP. They help you organize your code so you can update…

PHP Null Coalescing Operator: Handling Null Values

The PHP null coalescing operator, written as ??, is a helpful feature added in PHP 7. It makes it easier…

PHP Multidimensional Arrays: Data Manipulation

In web development, managing and manipulating data is one of the most common tasks. When it comes to storing complex…

PHP Predefined Constants Explained Simply

PHP has a number of predefined constants that give you some information about the language and its environment. You can…

Understanding the PHP Operator Precedence

The PHP operator precedence refers to when doing a calculation for three or more numbers, they are calculating its values…

PHP cURL: HTTP Requests and Responses in PHP

In PHP, there are plenty of times when you need to connect with other servers or APIs. That's where the…

PHP array_is_list: Check if an Array is a List with Examples

You need the array_is_list function to know if an array works as a list or not in PHP. What is…

PHP array_find: How to Locate Array Values with Examples

PHP array_find was released in PHP 8.4 to locate a value inside an array and returns the first match. Understand…

filter_has_var Function: Check if Input Exists in PHP

User input does not always come through as expected. Sometimes values are missing or not set at all. This can…

Polymorphism in PHP: How It Works with Examples

PHP 5 added support for classes and interfaces. This made object-oriented code possible with one feature being polymorphism. That means…

Previous Article

Mastering PHP's Do-While Loop: Examples and Explanation

Next Article

Understanding the PHP Operator Precedence

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.