views:

714

answers:

8

Why is === faster than == in PHP?

+42  A: 

Because the equality operator == coerces, or converts the data type temporarily to see if it's equal to the other operand whereas === ( identity operator ) doesn't need to do any converting whatsoever and thus less work is done, making it faster.

meder
@meder. I think your opinion is contrary with the what PHP Manual says. They say $a == $b is TRUE if $a is equal to $b, where $a === $b is TRUE if $a is equal to $b, and they are of the same type.
Bakhtiyor
How is it contrary, then?
meder
I believe it's actually that the 2 operands point to the same area of memory for complex types but meder's answer encompasses that
Basiclife
+2  A: 

I don't really know if it's significantly faster, but === in most languages is a direct type comparison, while == will try to do type coercion if necessary/possible to gain a match.

Chris
"=== in most languages"? Name two other langauges that know "===" - bad news for you. I have never seen "===" before in any other languagae.
TomTom
Javascript has the === operator.
Frank Shearar
I'm sure you can do === in common lisp and scheme.
J. Pablo Fernández
Javascript - not in 3 langauge definitions I checked ;) And Lisp and Scheme are many things, but hardly common ;)
TomTom
@tomtom: http://www.webreference.com/js/column26/stricteq.html for javascript, http://phrogz.net/programmingruby/language.html#table_18.4 for ruby, etc... Try not to be an ass, k?
Chris
ruby has ===. It has been too long for me to remember if it does the same thing.
KitsuneYMG
Also, http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html for actionscript. Basically, google "strict equality".
Chris
+8  A: 

First, === checks to see if the two arguments are the same type - so the number 1 and the string '1' fails on the type check before any comparisons are actually carried out. On the other hand, == doesn't check the type first and goes ahead and converts both arguments to the same type and then does the comparison.

Therefore, === is quicker at checking a fail condition

slightlymore
I'd guess that `==` also checks the type first to see if any type conversion needs to be done. The fact that `===` doesn't do any conversion in the following step is what makes it faster.
deceze
+1  A: 

Because === doesn't need to coerce the operands to be of the same type before comparing them.

I doubt the difference in speed is very much though. Under normal circumstances you should use whichever operator makes more sense.

therefromhere
+3  A: 

=== does not perform typecasting, so 0 == '0' evaluates to true, but 0 === '0' - to false.

Raveren
+1 for mentioning the two operators may evaluate differently.
Ioan
+1  A: 

The == incurs a larger overhead of type conversion before comparison. === first checks the type, then proceeds without having to do any type conversion.

Martin
A: 

In conclusion === is faster because don't converts the data type to see if two variables have same value, but when you need to see if two variables have same value you will use == if doesen't mather what type are variables, or === if is important also the type of variables.

D.Martin
A: 

identity operator is faster