In PHP, is there a short way to compare a variable to multiple values?

vertigoelectric picture vertigoelectric · May 2, 2013 · Viewed 11.3k times · Source

Basically what I'm wondering if there is a way to shorten something like this:

if ($variable == "one" || $variable == "two" || $variable == "three")

in such a way that the variable can be tested against or compared with multiple values without repeating the variable and operator every time.

For example, something along the lines of this might help:

if ($variable == "one" or "two" or "three")

or anything that results in less typing.

Answer

John Conde picture John Conde · May 2, 2013

in_array() is what I use

if (in_array($variable, array('one','two','three'))) {