6
votes

I know that !== is used to compare variable types too, while != only compares values.

But I see that many people use !== when they compare values, for example:

$stuff = 'foo';
if($stuff !== 'foo') // do...

Is there any reason they do this? Is !== faster than != or what?

7
Use the one that is logically suited at the certain situation. Don't prefer one over another because of "performance".kapa

7 Answers

3
votes

If you do know in advance that both variables are of the same type, then it won't make any difference. I think the speed difference is just so negligible that the speed argument can (and should) be completely ignored, and you should focus on your application rather than focusing on premature (and unnecessary) optimization.

However, if you do have a requirement that and argument should be exactly what you expect (for example, === false when you don't want 0 to pass the equality test), then use === or !==.

As mentioned by Headshota, if you are working with objects, then === checks that the two variables actually point to the same object instance, whereas == tries to match them by value, which might lead to unexpected results and should be used with great care. Here the strict operator will be used because it means something to the application.

To answer your question, if in your particular context some people are using === or !== where you do know that both variables are of the same (scalar) type, then I guess it's just a matter of coding style, and shows a bit of strictness in the code, enforcing the idea that both variables are of the same type, probably not with performance in mind.

The strict operators have to be used when it means something to do so, not as a performance optimization.

Update: to reply to the speed argument, let's consider this small benchmark:

$a = "test";
$b = "test";

$t = microtime(true);
for ($i=0; $i<1e6; $i++) {
    ($a == $b);
    // (100x the equality test, omitted for clarity purposes,
    //  to lower the speed impact of the loop itself)
    ($a == $b);
}
printf('%.3f', microtime(true)-$t);

I roughly get:

6.6 seconds for ==
4.3 seconds for ===

Now if I use $b = 1; to force an implicit conversion, I get roughly:

4.4 seconds for ==
2.4 seconds for ===

This means roughly a 2 seconds gain in both cases. You might think, and you will be right in some way, that this is a demonstration of the speed advantage of the strict comparison operator. But don't forget that we are talking about 1e6 * 100 = 100 million iterations.

That means that a single strict comparison will speed up your script by 0.00000002 seconds.

If you think that this is something you should consider when writing your script, then do so. But you'll soon encounter other tiny optimizations that will give you 1000 times this speed gain, and make such "optimizations" look useless. This is a textbook case of premature optimization.

Again, use the strict comparison operators if you have to, because it is a requirement in your code. Do not use it for performance reasons.

13
votes

It is wrong to say that != does only compare the value. Because it rather compares the types, applies a type conversion if necessary (see table of Comparison with Various Types) and then compares the (converted) values.

In contrast to that, !== fails earlier if the types are not equal and no type conversion is done.

5
votes

The != operator is a bit slower because it includes an implicit conversion if necessary,

while !== compares the types before and then the values (only if types are equals)

Also, follow this answer for further infos on the differences between the two operands

2
votes
For example

$flag = 0;
if($flag == false ){
   echo "true";
}else{
   echo "false";
}                      // echo true;


$flag = 0;
if($flag === false ){
   echo "true";
}else{
   echo "false";
}                     // echo false;


$flag = false;
if($flag === false ){
   echo "true";
}else{
   echo "false";
}                    // echo true
2
votes

Actually, !== is not used to compare variable types, it is used to ensure that the values being compared are the same type as well as have the same value.

So if you have

$i = "0";

$i == 0 will return true, while $i === 0 will return false.

if you have

$j = 0;

$j == null will return true, while $j === null will return false.

2
votes

It's not really faster when you know the type is the same, which is the case in your example. The major reason people use !== is that it keeps PHP from doing its type-juggling magic, so there's less chance of the comparison meaning something you didn't want it to.

1
votes

It is generally safer to check identity, than equality, this avoids many errors caused by invalid types. but there are cases when identity is not a must, so you can use equality in this cases.