Check if a variable is different from multiple values

If I want to take action, if php, if the variable is not 0.1 or 2, how would I do it? my if statement does not work. Thank!

+3
source share
3 answers
if (($var != 0) && ($var != 1) && ($var != 2)) {
    //...
}

or...

if (!in_array($var, array(0, 1, 2))) { /* ... */ }

See logical operators .

+7
source

The easiest way:

if ($x != 0 && $x != 1 && $x != 2)
{
    // take action!
}

If you know your variable is int, you can also do the following:

if ($x < 0 || $x > 2)
{
    // take action!
}
+5
source

&& !==, :

if( $n !== 0 && $n !== 1 && $n !== 2 ) {
     // it not any of those values.
}

== , :

  • 0 == 'foo'
  • 99 == '99balloons'
  • true == 1
  • false == ''

. . == vs. ===. " ", , , , . , if( $n < 0 || $n > 2 ) . (, , .)

&& ||.

0

Source: https://habr.com/ru/post/1760849/


All Articles