php - Checking one variable against multiple values? -



php - Checking one variable against multiple values? -

currently, i'm doing this:

class="lang-php prettyprint-override">if ( in_array ( $variable, ["a","b","c"] ) ) { ... }

which reads little easier than

if ( $variable == "a" || $variable == "b" || $variable == "c" ) { ... }

but wondering, there more efficient ways, instead of checking value in array?

try this

$my_array = array_flip(array('a', 'b', 'c', 'd', ...)); if (isset($my_array[$variable])) ...

this has one-time o(n) cost create $my_array, checking match o(1).

php arrays if-statement

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

Hibernate criteria by a list of natural ids -

ios - Lagging ScrollView with UIWebview inside -