PHP - Checking for return false;

There is a case where the use of the single ! prefix operator would not involve any risk and it is when the type of the expression to evaluate is always a boolean.

For example, the expression below

if (!is_array($var)) {

will always behave similarly to

if (is_array($var) === false) {

This is because the function is_array always returns a bool as result.

is_array ( mixed $var ) : bool

Some people may find easier to read the first expression with the single ! prefix operator. The only thing to have in mind when using this is that when used with expressions where the returned type is mixed we may have unexpected results as other comments already pointed.


They are similar, but not exactly the same. The !$check is a loose checker, meaning 0 and null would evaluate to true as well. The === is a strict checker, meaning it must be false.

For a more indepth explanation: PHP Types Comparisons


=== false checks type, (!$check) evaluates the value and will fail for values returned such as null, '' (empty string) and zero(0)

=== is most correct


The triple-equal operator is type-sensitive. So when you check:

if ($check === false)

... it will only be true if $check is the boolean value "false". Wheras

if ($check == false)

... is not checking specifically for boolean false, but a "falsey" value. False, in PHP, equals zero, null is "falsey", as is an empty string ("" == false == null, but "" !== false !== null). So:

$check = 0;
if ($check == false)

... evaluates to true.

The ! prefix operator is equivalent to ==. So when zero needs to be a discrete value from boolean false, the ! and == operators are not sufficient.

Check out the docs for comparison operators here: http://php.net/manual/en/language.operators.comparison.php

It is best practice to make your conditional checks as specific as possible. This is not only to avoid potential oversights in your logic, but also to make the code more maintainable for future developers. Imagine encountering a line of code like this:

if (check_something($variable)) {
  // do stuff
}

I can assume the check_something function is returning a boolean value true. But, unless I go dig up the check_something function, it could also be returning an non-empty string, a number... who knows! Much more clear to do this:

if (check_something($variable) === true) {
  // do stuff
}

Now, just by looking, I know that the check_something function is expected to return a true value. I might not know what the function does, but at least it is exactly clear what it returns. Another common example you see EVERYWHERE:

if (!$_GET['value']) {
  // do something
}

This is a pet peeve. A conditional statement should always be comparing things clearly. So, you'd want to do:

if (array_key_exists('value', $_GET) !== false && $_GET['value'] === '1') {
  // do something
}

Now, you can tell that I am not only checking to see if the query string parameter exists, but also whether it is equal to a specific value.

In summary, the single ! prefix operator and the == operator are rarely useful and always ambiguous. You're best served by writing your code in a way that documents itself, or is translatable into human language to express the logic at play. A direct comparison using either !== or ===, where appropriate, is a good habit, good practice, and the least likely to produce unexpected results in your code.

Tags:

Php