New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL
CLOSE
   PHP  HowTo  
   Technology    Programming

How to use 'OR' in switch case condition in PHP?

         
 JUNE 26, 2020   by iamabhishek

When we use a switch case in PHP, mostly the requirement is to evaluate a single condition in every case. Here is a simple example where we have used the switch statement in PHP to print something based on the choice of the car provided:

<?php

switch($car)
{
    case "Audi":
        echo "Audi is amazing";
        break;
    case "Mercedes":
        echo "Mercedes is mindblowing";
        break;
    case "Jaguar":
        echo "Jaguar is the best";
        break;
    default:
        echo "$car is Ok";
}
?>

In the code example above, we have specified a single condition, like if the car is "Audi" do this, and if the car is "Mercedes" do something else. But what if you want to perform the same action, if the car input is either "Audi" or "Mercedes".

Hence the question arises, Can we use an 'OR' operator or equivalent in a PHP switch?

Well, the answer is Yes, we can.

Using 'OR' operator in PHP Switch case:

If you want to use || (OR operator) with switch then you can try:

<?php

switch(true)
{
    case ($car == "Audi" || $car == "Mercedes"):
        echo "German cars are amazing";
        break;
    case ($car == "Jaguar"):
        echo "Jaguar is the best";
        break;
    default:
        echo "$car is Ok";
}
?>

Notice in the above code, we haven't passed the $car variable to the switch statement, rather passed the value true to it, so that it executes until the break statement. And in the case block, we have used the $car variable for matching it against multiple values using the OR operator.

Without using 'OR' operator in PHP Switch case:

If you do not want to use the OR operator and still want to execute the same code statement for more than one case, then this will work for you well:

<?php

switch($car)
{
    case "Audi":
    case "Mercedes":
        echo "German cars are mindblowing";
        break;
    case "Jaguar":
        echo "Jaguar is the best";
        break;
    default:
        echo "$car is Ok";
}
?>

As you can see in the code above, we have provided stacked two case blocks together, if any of them matches then the echo statement will be executed.

Similarly, you can use this PHP code for your use case.

Range of value in Switch case:

We can even use a range of values in a switch case. For example,

$range1 = range(1, 100);
$range2 = range(100, 200);
$value = 76;
switch (true) {
    case in_array($value, $range1) :
        echo 'the number is between 1 to 100';
        break;
    case in_array($value, $range2) :
        echo 'the number is between 100 to 200';
        break;
}


the number is between 1 to 100

But for complex conditions, it's better to use if-else conditions in PHP.

Conclusion:

So in this tutorial, we learned how we can provide multiple conditions in switch case in PHP using the OR operator so that we can define the same code statements for more than one case in switch statement in PHP.

If you have some specific usecase, feel free to share with us in the comment section below.


RELATED POSTS



Subscribe and receive amazing posts directly in your inbox.