

Yes, 'OR' can be used in switch case conditions in PHP to match multiple cases.Ģ.

Can 'OR' be used in switch case conditions in PHP? If you have some specific use cases, feel free to share them with us in the comment section below.
#Php if else inside switch statement code
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. For example, $range1 = range(1, 100) īut for complex conditions, it's better to use if-else conditions in PHP. We can even use a range of values in a switch case. Similarly, you can use this PHP code for your use 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: Īs 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. Without using 'OR' operator in PHP Switch case: And in the case block, we have used the $car variable for matching it against multiple values using the OR operator. 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. If you want to use || (OR operator) with switch then you can try: Hence the question arises, Can we use an 'OR' operator or equivalent in a PHP switch? But what if you want to perform the same action, if the car input is either "Audi" or "Mercedes". 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. 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: When we use a switch case in PHP, mostly the requirement is to evaluate a single condition in every case.
