Skip to content
Advertisement

PHP Check if variable is contained in an array

I have a numeric variable $i. This is a while loop, and $i increments after each iteration.

How can a conditional statement be made as to not be necessary to write such a long statement if($i == 1 || $i == 2 || $i == 25 [...])?

Thanks in advance!

Advertisement

Answer

Use php in_array.

$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement