PHP: if a is equal to b or c or d

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

it

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

the shortest way to describe this logic. I think of something like

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

but this is invalid code. Any suggestions?

+5
source share
4 answers

You can use in_array :

if( in_array($a, array($b,$c,$d)) ){
    //do something
}
+11
source

This is valid code, but not logically correct.

If you have a lot of meanings, you can do something like this.

if(in_array($a,array($b,$c,$d))) {
}
+2
source

This is not the same as being ||a logical operator and will always return trueor false. So, in the second expression, you are comparing if $a- trueor false.

You can use in_arrayto compare if $aexists inarray($b, $c, $d)

+1
source

I don’t know why you want to do something, but you can put b, c, d in an array and call the in_array function to find the elements. However, I cannot understand why you want the short and simple code to be made short and complicated.

+1
source

All Articles