If "car" or "ferrari" as an input, it should print "car or ferrari". How can I achieve it?
<?php
$car ='333';
switch($car)
{
        case car OR ferrari:
                print("car or ferrari");
                break;
        case cat:
                print("cat");
                break;
        default:
                print("default");
                break;
}
?>
                Use two case clauses:
case 'car':
case 'ferrari':
    print("car or ferrari");
    break;
The explanation:
It is important to understand how the
switchstatement is executed in order to avoid mistakes. Theswitchstatement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when acasestatement is found with a value that matches the value of theswitchexpression does PHP begin to execute the statements. PHP continues to execute the statements until the end of theswitchblock, or the first time it sees abreakstatement. If you don't write abreakstatement at the end of a case's statement list, PHP will go on executing the statements of the following case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With