-4

I know this topic has been talked about before here, but this case was not mentioned, and I'm still not positive which is better.

If you have an if statement that is something like:

if (($var=='A') || ($var=='B') || ($var=='C') || ($var=='D')...) { echo "hello world"; }

In this situation, would it be better to use a switch statement?

Thanks!

beeshko
  • 5
  • 2

6 Answers6

2

From my understanding of the question i think i would be better to use in_array(); When you want to check if this or that is true then do the same thing you can make an array and then check if it's in there and do something like so:

$array=array("A","B","C","D");
if(in_array($var,$array)){
   echo 'hello world';
}

In the case you want to do different thing depending on the variable, then use switch, something like:

$lang='fr';
switch($lang){
   case 'fr': echo "français"; break;
   case 'sp': echo "spanish"; break;
   default: echo "english"; break;
}

Finally use the if to check specific comparison, let say:

if($lang=='fr'){
  echo "bonjour le monde";
}else{
  echo "hello world";
}
Louis Loudog Trottier
  • 1,367
  • 13
  • 26
1

You can use switch in this case too and if the same value exists then do not add break for the cases with same value as below:

switch ($var) {
    case 'A':
    case 'B':
    case 'C':
    ........ //Any other cases
        echo "hello world";
        break;
    default:
        echo "something else";
        break;
}
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
1

I don't really think you need to think about performance for this kind of thing, but if you want a more performance-oriented response, look at this post: What is the relative performance difference of if/else versus switch statement in Java?

If you just want your code to be organized, I would use:

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}
?>

Source: http://php.net/manual/en/function.in-array.php

Community
  • 1
  • 1
KodornaRocks
  • 425
  • 3
  • 14
0

First off, in your example, you assign instead of compare.
Assignment is done with one equal-sign (=) while comparison is done with two or (rather) three, in php (===).

A switch would maybe be slightly shorter, but not necessarily make the code any more readable or maintainable.
If ANY of your checks are true, it should print "hello world", which means that the switch would either use a lot of fall-through, which is kinda ugly (and raises warnings in a bunch of IDEs), or require you to have the echo "hello world"; in all cases, which would add an extra line for each case.

Personally, in a case like this, I would use an array with the values to check for, then use the in_array function in the if statement.
Something like:

$array = ['A', 'B', 'C']; // and so on.
if(in_array($val, $array)) {
  echo "hello world";
} 

Edit:
Also worth noting, about switches, is that a switch does no typecheck when it compares.
That is, when a switch compares it uses == not ===.

Jite
  • 5,761
  • 2
  • 23
  • 37
0

If you ever think you might want to do something other than echo "hello world" for each condition, then yes, I'd use a switch. I'd also use the switch if I had to check for 12 letters of the alphabet. Otherwise, forget the switch. The if statement is obviously really handy for combining expressions, too. Personally, I break up really long expressions like this:

if ( $var = 'A'
     ||
     $var = 'B'
     ||
     ( 
         $var = 'B' 
         && 
         $var = 'C' 
     )
     ||
     $var = 'D'
    ) {
    echo "hello world";
}
Ben T
  • 202
  • 1
  • 4
0

there are 2 points regarding your question.

1: use double equal signs (==) with if condition, other-wise the condition will always true.

2: You can use switch case statement, if you like to do.

switch ($var) {
  case 'A':
   case 'b':
    case 'c':
     case 'd':
        echo "hello world";
    break;
  default:
    // Default case
    break;
}
Samit Khulve
  • 154
  • 6