Home » Php » if statement – PHP: If a equals b or c or d

if statement – PHP: If a equals b or c or d

Posted by: admin July 12, 2020 Leave a comment


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?

How to&Answers:

You could use in_array:

if( in_array($a, array($b,$c,$d)) ){
    //do something


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))) {


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)


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.