30

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 :)

0

8 Answers 8

60

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.

3
  • 3
    BTW, this is a problem specific to php (and perhaps other loosely typed languages), due to its extreme willingness to automatically convert values from one type to another. In strongly typed languages (e.g. Java, C#, C++), most programmers prefer concise syntax such as !value ("not value") instead of value == false or value === false. In those languages, the compiler will complain if value is not a Boolean type, so the concise form is safe. "not value" has a clear meaning, both in logic and in human language; it is only the "looseness" of php that makes this form poor to use. May 13, 2016 at 19:03
  • ^ and javascript to an extent May 13, 2016 at 19:27
  • takes tonns of time to find it so deep in code
    – CodeToLife
    Dec 29, 2020 at 17:59
7

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

=== is most correct

4

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

1

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.

0

== 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 )
2
  • 1
    === compares value and type of the 2 things. It's not bitwise.
    – gen_Eric
    Jun 3, 2011 at 19:45
  • It's the first time I heard about bitwise regarding the === operator. More info could be helpful.
    – JohnnyQ
    Jun 8, 2016 at 4:35
0

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

1
  • This would fail for the same reasons as using !$check. He really should use $check !== FALSE.
    – gen_Eric
    Jun 3, 2011 at 19:47
0

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.

0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.