tags:

views:

306

answers:

5

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;
}
?>
+2  A: 
switch($car)
{

        case car:
        case ferrari:
                print("car or ferrari");
                break;
        case cat:
                print("cat");
                break;
        default:
                print("default");
                break;
}

Cases "fall through" until the first break statement. This also means that you don't need a break in the default case.

Tomalak
+4  A: 

You can simply "fall through" the cases you want to handle equally:

    <?php
    $auto ='333';
    switch($auto)
    {

    case car:
    case ferrari:
            print("car or ferrari");
            break;
    case kissa:
            print("cat");
            break;
    default:
            print("default");
            break;
    }
Cassy
How do you respond to the critique by the handsomeGun?
HH
Sorry, I might be too late, but I cannot find critique from handsomeGun?
Cassy
+8  A: 

Use two case clauses:

case 'car':
case 'ferrari':
    print("car or ferrari");
    break;

The explanation:

It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case.

Gumbo
+1 and accepted because you targeted critique. Thank you.
HH
+1  A: 
switch($car) {
case 'car':
case 'ferrari':
    print("car or ferrari");
    break;
case 'cat':
    print("cat");
    break;
default:
    print("default");
    break;
}

This is taking advantage of the 'fall-through' property of the switch() statement. Basically, a section doesn't stop at a case, it stops at a break (or other operation that exits the function).

I've taken the liberty of applying the indentation I prefer for switches, which makes them only use up one indent level, which I consider appropriate because, logically, the switch and its cases are all elements of the same construct. So using two indent levels within the switch conveys no useful information.

chaos
+1  A: 

I dont' know if PHP supports this but in C, you could do something like this:

case car:
case ferrari:
            print("car or ferrari");
            break;

The idea is that code for handling the car case will keep running until it hits a break statement. As for style, it should be avoided.

llamaoo7
+1 for starting comparison
HH