Check if variable is different from several values
11,325
Solution 1
if (($var != 0) && ($var != 1) && ($var != 2)) {
//...
}
or...
if (!in_array($var, array(0, 1, 2))) { /* ... */ }
See logical operators.
Solution 2
The most straightforward way:
if ($x != 0 && $x != 1 && $x != 2)
{
// take action!
}
If you know your variable is an int, then you could also do like:
if ($x < 0 || $x > 2)
{
// take action!
}
Author by
Leticia Meyer
Updated on June 04, 2022Comments
-
Leticia Meyer almost 2 years
If I want to take an action if php if a variable isn't 0,1, or 2, how would I do that? my if statement isn't working. Thanks!