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

Date: 9 Mar 2010 Comments: 0

First, let’s remember the definitions of the operators: The Equal operator ($a == $b) returns TRUE when  $a is equal to $b.

The Identical ($a === $b)  returns TRUE when $a is equal to $b, and they are of the same type.

When we use the identical operator $a === $b, first it checks to see if the two arguments ($a and $b) are the same type.
So, if $a is 1 and $b is ’1′ the check will fail on the type checking, before and any comparison are actually carried out.

Another reason the equal operator ($a == $b) to be slower than the identical operator is that the equal operator first goes ahead and it converts both arguments ($a and $b) to the same type and does the comparison.

Leave a Reply