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.
How to use 'OR' in switch case condition in PHP?
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
Harnessing the power of 'OR' in switch case conditions empowers PHP developers to handle multiple cases with a single code block, streamlining their code and enhancing its readability. By leveraging this flexible feature, you can reduce redundancy and ensure that your code executes accurately and efficiently.
So in this tutorial, we learned how we could provide multiple conditions in switch cases in PHP using the OR operator so that we can define the same code statements for more than one case in switch statements in PHP.
If you have some specific use cases, feel free to share them with us in the comment section below.
Frequently Asked Questions(FAQs)
1. Can 'OR' be used in switch case conditions in PHP?
Yes, 'OR' can be used in switch case conditions in PHP to match multiple cases.
2. How do I use 'OR' in the switch case condition in PHP?
To use 'OR' in the switch case condition, separate the cases with the 'OR' operator.
3. Can I use other operators in the switch case condition in PHP?
Yes, other logical operators like 'AND' and 'NOT' can also be used in switch case conditions in PHP.
4. What is the advantage of using 'OR' in switch case conditions in PHP?
Using 'OR' in the switch case condition can simplify the code and make it more efficient by reducing the need for multiple switch statements.
5. Can I use 'OR' in combination with 'AND' in a switch case condition?
Yes, you can combine 'OR' and 'AND' operators in a switch case condition to create more complex conditions. By properly using parentheses to group the conditions, you can define intricate logic that determines the execution of the code block.
You may also like: