Search code examples
phpoperatorscomparison-operators

Identical operator strange behaviour


Can anyone explain me this strange behaviour of the "identical" operator in php?

$any_integer_but_not_one = -1;

var_dump( !$any_integer_but_not_one === 1 ); // bool(false)
var_dump(  $any_integer_but_not_one !== 1 ); // bool(true)

$int_one = 1;

var_dump( !$int_one === 1 ); // bool(false)
var_dump(  $int_one !== 1 ); // bool(false)

I expected that these tests will always return the same results (true-true or false-false), but they do not.

I thought that !x === y and x !== y do the same thing, so what is the difference? Here I am talking only for the identical operator, not the equal one.
(Any other questions I found were about differences between equality and identity)


Solution

  • its because -

    !$any_integer_but_not_one will return false.

    when var_dump( !$any_integer_but_not_one === 1 ); it is var_dump( false === 1 );

    so it will return false