7

Possible Duplicate:
Short hand to do something like: if($variable == 1 || $variable == “whatever” || $variable == '492') .

Is this

if ($a==b||$a==c||$a==$d){ ... 

the shortest way to describe this logic. I am thinking about something like

if ($a==($b||$c||$d)) { ...

but that is not a valid code. Any suggestions?

Community
  • 1
  • 1
Martin
  • 1,193
  • 3
  • 12
  • 24

4 Answers4

13

You could use in_array:

if( in_array($a, array($b,$c,$d)) ){
    //do something
}
Engineer
  • 47,849
  • 12
  • 88
  • 91
2

That is valid code but not logically correct.

If you have lots of values then you could do something like this.

if(in_array($a,array($b,$c,$d))) {
}
bigkm
  • 2,218
  • 1
  • 16
  • 12
1

That both are not same as || is boolean operator and will always return true or false. So in second statement, you are comparing if $a is true or false.

You can use in_array to compare if $a exists in array($b, $c, $d)

Riz
  • 9,703
  • 8
  • 38
  • 54
1

don't know why You want to do some sort of thing, but You can put b,c,d in array and call in_array function to search for elements. Still I can't understand why You want to short and simple code make short and not simple.

pawel-kuznik
  • 437
  • 4
  • 11