'PHP - Checking for return false;

Just a quick question - and I'm sure really basic!

I have the following code:

function checkThings($foo, $bar) {
    ...
    if ($valid) {
        return $results;
    } else {
        return false;
    }
}

On the other end of this I am currently doing

$check = checkThings($foo, $bar);
if ($check === false) {
    echo "Error";
} else {
    echo $check;
}

Is writing the following the same?

$check = checkThings($foo, $bar);
if (!$check) {
    echo "Error";
} else {
    echo $check;
}

Which method is the preferred method if both are correct?

Thanks :)

php


Solution 1:[1]

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.

Solution 2:[2]

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

=== is most correct

Solution 3:[3]

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

Solution 4:[4]

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.

Solution 5:[5]

== does a value comparison where === does a bitwise.

// will return true only when $value = false
if( $value === false )

// will return true when $value = 0, $value = "", $value = null, etc
if( $value == false )
if( !$value )

Solution 6:[6]

I would prefer your second method, but put the positive check first (if($check) instead).

Solution 7:[7]

If you are trying to check for multiple conditions, you can do this:

function checkThings($foo, $bar)
{
    else if ($valid === false)
    {
        return false;
    }
    else if ($valid === 1)
    {
        return 1;
    }
    else if ($valid === 2)
    {
        return 2;
    }
    else if ($valid === 3)
    {
        return 3;
    }
}

But you must strictly check the return of the function with "===" when you are attempting to check which value is returned and what piece of code ran.

Solution 8:[8]

in my case when I ve got a value from JS Datatable I checked it as:

if($value=='true'){ // or for 'false' }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 David Chan
Solution 3 Jim
Solution 4
Solution 5 Ben Roux
Solution 6 MarkD
Solution 7 BlackCoder
Solution 8 CodeToLife