PHP comparison operator precedence and inequality

Here’s a thought exercise, can you tell exactly under what conditions is the following statement true, and when is it false?

$var = get_var();
if( ! 'some value' === $var && ! 'some other value' === $var ) {
  echo "Do the thing";

At first, you might think that the condition will only execute, if $var turns out not to be 'some value'and 'some other value' if that was you, take another look, then read on 🙂

The real answer is - “Do the thing” is only going to run when $var is false because ! "Do the thing" is evaluated first, and it’s cast to a boolean so that PHP can invert it, resulting in false.

In other words, the condition above, is the same as:

if( false === $var ) {
  echo "Do the thing";

Here’s another example to illustrate how the exclamation point ( ! ) works in PHP conditions:

// Arrays not equal
> ['a', 'b'] == ['a', 'b', 'c']

// Arrays are equal
> ['a', 'b'] == ['a', 'b']

// Array is cast to false by !
> ! ['a', 'b']

// Another example of array cast to false
> ! ['a', 'b', 'c']

// This will cast only 1 array and compare false to the other array
> ! ['a', 'b'] == ['a', 'b', 'c']

// Same as above
> ! ['a', 'b'] == ['a', 'b']

The lesson here is to be explicit - if you want to negate a comparison - don’t try to save on file size by omitting parenthesis. Or better yet, use strong inequality check directly:

// As we established, this is a bug:
! 'value' === $var

// But instead of this
! ( 'value' === $var )

// Do this:
'value' !== $var