0

I have two statements like this:

$ready = true;

if($ready === true) {
    echo "it's ready!";
}

And this:

$ready = true;

if($ready !== false) {
    echo "it's ready!";
}

I know those mean the same thing as === true and !== false is the same thing. I see people use the negation type (is not false or is not {something}). like this: https://stackoverflow.com/a/4366748/4357238

But what are the pros and cons of using one or the other? Are there any performance benefits? Any coding benefits? I would think it is confusing to use the negation type rather than the straight forward === true. Any input would be appreciated! thank you

Timmy Balk
  • 238
  • 3
  • 14
  • 2
    `===true`, `!==false`, `==true` and `!=false` are actually four separate, distinct things. – paulsm4 Mar 04 '19 at 04:54
  • @paulsm4 thank you but I know the difference between those. I was just asking if there was a difference in using double negatives of something (not false) rather than positive (true). – Timmy Balk Mar 04 '19 at 07:01

4 Answers4

3

These do not mean the same thing, and you can see this demonstrated below:

$ready = undefined;

if($ready === true) {
  console.log("it's ready 1");
}

if($ready !== false) {
  console.log("it's ready 2");
}

When in doubt, stick with ===. You're comparing to exactly what it should be. "This should exactly be equal to true" vs "This should be equal to anything but exactly false" are 2 separate statements, the second including a lot of values that would be true (As demonstrated above, undefined is not exactly false, so this would be true).

Blue
  • 22,608
  • 7
  • 62
  • 92
1

Most PHP functions return false as failure, such as strpos, from the manual it says:

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

So it returns an integer or false, then it is wrong to use strpos() === true

strpos() !== false is the right way to check the result.

shingo
  • 18,436
  • 5
  • 23
  • 42
0

It is not about performance and they are not the same. It helps us in reducing unnecessary if else statements.

Consider this: there is a variable foobarbaz whose value can be anything among foo/ bar/ baz/ foobar/ barbaz. You need to execute a statement if the value is not foo. So instead of writing like this:

if(foobarbaz === "bar" || foobarbaz === "baz" || foobarbaz === "foobar" || foobarbaz === "barbaz") {
    //some statement
}
else if(foobarbaz === "foo") {
    //do nothing
}

You can write something like this:

if(foobarbaz !== "foo") {
    //some statement
}

You can see we were able to eliminate the unnecessary else statement.

Prasanth Ganesan
  • 541
  • 4
  • 14
0

In php == compare only the values are equal. For and example

$ready = 'true';

if($ready == true) {
    echo "true";
}
else{
    echo "false";
}

It print true.But if it is compare with === it not only check the values but also it check the datatype. So it compares with === it print the false.

Talking about !== it's work as the same. So it's not a problem to output if you use

if($ready === true) 

or

if($ready !== false)

Can't compare these two because both are same.

Hasee Amarathunga
  • 1,901
  • 12
  • 17