Shorthand expression for the if statement ($ a == $ b || $ a == $ c)

I know this code will work:

echo ( $a == $b || $a == $c ) ? "Yes" : "No";

This can be read as:

if $ a equals $ b or $ a equals $ c

Is there a way to make it shorter, for example:

if $ a is $ b or $ c

I tried a lot, including this, but still no luck:

echo ( $a == ( $b xor $c ) ) ? "Yes" : "No";
+4
source share
2 answers

You can use in_array:

var_dump(in_array($a, [$b, $c]));

with your example:

echo in_array($a, [$b, $c]) ? 'Yes' : 'No';

Note: this syntax is only useful if there are more than two values. For a few values, it $a == $b || $a == $cworks well and is probably faster.

+7
source

, , , , .

preg_match('/^('.$b.'|'.$c.')$/',$a) === 0

in_array($a,array($b,$c)) === true

, PHP .

+1

All Articles