How to use a PHP Switch Case with Multiple Conditions?

Summary

In this tutorial, we will examine the syntax, parameters, differences from the if statement, and provide you with a variety of examples to showcase the versatility of the PHP switch case statement.

Introduction

In the world of PHP programming, the switch case statement serves as a powerful tool for handling multiple conditions within your code. While the if statement is a commonly used construct, the switch case offers a more concise and structured way to manage complex branching logic.

Syntax and Parameters

 The PHP switch case statement follows a straightforward syntax:

<?php 

switch ($variable) {

case value1:

     // Code to execute for value1

     break;

case value2:

     // Code to execute for value2

     break;

// additional cases

default:

     // Code to execute if no case matches

}

Here, $variable represents the value you are evaluating, and each case corresponds to a potential value of the variable. The break statement terminates the switch case block, preventing fall-through to subsequent cases.

Note: Like break, the continue statement also applies to switch. Use continue 2 to move to the following iteration of the outer loop if there is a switch inside a loop.

How to work?

The switch statement works step by step. It starts running when it finds a case that matches the switch expression’s value. PHP keeps running the code until the switch section ends or it encounters a “breakstatement. If you forget to use “break” at the end of a case, PHP will keep running code from the next case.

<?php

// Represents Wednesday

$dayNumber = 3; 

switch ($dayNumber) {

case 1:

     echo "It's Monday!";

     break;

case 2:

     echo "It's Tuesday!";

     break;

case 3:

     echo "It's Wednesday!";

     // No "break" here, so it will continue to the next case

case 4:

     echo "It's Thursday!";

     break;

default:

     echo "It's a weekend day!";

}

Result

It's Wednesday!
It's Thursday!

Examples:

Let us explore some practical examples to illustrate the usage of the PHP switch case with multiple conditions:

Example 1#

<?php 

$score = 85;

switch ($score) {

case ($score >= 90):

     echo "A";

     break;

case ($score >= 80):

     echo "B";

     break;

case ($score >= 70):

     echo "C";

    break;

default:

     echo "D";

}

Result

B

Example 2#

<?php 

//Day of the Week:

$day = "Wednesday";

switch ($day) {

case "Monday":

case "Tuesday":

case "Wednesday":

case "Thursday":

case "Friday":

     echo "Weekday";

     break;

case "Saturday":

case "Sunday":

     echo "Weekend";

     break;

default:

     echo "Invalid day";

}

Result

 Weekday

Frequently Asked Questions

Q: How to use multiple switch case in PHP?

Ans: To use multiple switch cases in PHP, follow these steps: define the variable to be evaluated, use the switch statement, and provide cases for different values. Each case should contain the desired code block, and do not forget the break statement to prevent fall-through.

Q: Can you use a switch case with multiple conditions?

Ans: Switch cases in PHP are primarily designed for single-value comparisons. While you cannot directly use multiple conditions within a single case, you can achieve similar results by nesting switch statements or combining the cases under the same code block.

Q: What is the switch case in PHP with condition?

Ans: In PHP, the switch case is a control structure used to compare a variable against multiple values. When a match is found, the corresponding code block executes. The “with condition” implies that you can utilize the evaluated value to determine the case, allowing for flexible control flow.

Q: Can we use switch case in PHP?

Ans: Yes, you can use the switch case in PHP. It is a powerful tool for simplifying complex decision-making based on the value of a variable. Switch cases improve code readability and efficiency, especially when dealing with multiple possible values for comparison.

Conclusion:

In the world of PHP programming, overcoming the switch case statement with multiple conditions can greatly improve your ability to handle complex decision-making processes. Its structured syntax, along with the ability to handle different cases efficiently, make it an invaluable tool for writing neat and organized code. 

%d bloggers like this: