PHP switch case more than one value in the case

The simplest and probably the best way performance-wise would be:

switch ($var2) {
    case 1:
    case 2:
       $var3 = 'Weekly';
       break;
    case 3:
       $var3 = 'Monthly';
       break;
    case 4:
    case 5:
       $var3 = 'Quarterly';
       break;
}

Also, possible for more complex situations:

switch ($var2) {
    case ($var2 == 1 || $var2 == 2):
        $var3 = 'Weekly';
        break;
    case 3:
        $var3 = 'Monthly';
        break;
    case ($var2 == 4 || $var2 == 5):
        $var3 = 'Quarterly';
        break;
}

In this scenario, $var2 must be set and can not be null or 0

Leave a Comment