0

Possible Duplicate:
Switch vs if statements

In a very simple php scenario, what are the benefits (if any) of :

function foo_1($bar){
  if $bar == 'yes'{
  return 'yes';
  } 
// or the use of 'else'
 if $bar == 'no'{
 return 'no';
  }
}

against :

function foo_2($bar){
 switch ($bar) {
    case 'yes' :
     return 'yes';
    break;

    case 'no' :
     return 'no';
    break; 
    }
  }
}

(SIDENOTE : I know that if we have multiple conditions ( if ($bar=='yes') && ($bar !='else')..) that might be a reason, but also in that scenario, one can have case with conditions inside ..)

Community
  • 1
  • 1
Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105

1 Answers1

1

It's a question of preference, since both if and switch can provide the same functionality. I'd ask myself which option makes your code more readable.

Manny Ramirez
  • 149
  • 1
  • 6