Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a more simple way to code in php "if ( $x == $a[1] || $x == $a[2] || $x == $a[3] ....)

As the title states, whats a more simple way to do

if ( $somevariable == $somearray[1] || $somevariable  == $somearray[3] || $somevariable  == $somearray[10] ) 

Seems like even with 3 variables.. there would be a shortcut.

I know this doesnt work, but something like would be nice:

if ($somevariable == $somearray[1],$somearray[3],$somearray[10]) {
like image 463
Roeland Avatar asked Dec 07 '22 22:12

Roeland


2 Answers

One option

if ( in_array( $somevariable, array( $somearray[1], $somearray[3], $somearray[10] ) ) {}
like image 113
Galen Avatar answered Dec 10 '22 11:12

Galen


You could use in_array.

like image 28
Nick Van Brunt Avatar answered Dec 10 '22 13:12

Nick Van Brunt