Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Why would you use === instead of == PHP?

Both are comparison operators used to compare two or more values. == Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. === Operator: This operator is used to check the given values and its data type are equal or not.

Why do we prefer === and !== Over == and != In JavaScript?

The difference between == and === is that: == converts the variable values to the same type before performing comparison. This is called type coercion. === does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.

How does == vs === differ?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

What is faster in PHP?

single quote is generally faster, and everything quoted inside treated as plain string, like echo 'anyting else ? $ something'; >> anything else? $ something. PHP won't use additional processing to interpret what is inside the single quote.


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


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


There are two things to consider:

  1. If operand types are different then == and === produce different results. In that case the speed of the operators does not matter; what matters is which one produces the desired result.

  2. If operand types are same then you can use either == or === as both will produce same results. In that case the speed of both operators is almost identical. This is because no type conversion is performed by either operators.

I compared the speed of:

  • $a == $b vs $a === $b
  • where $a and $b were random integers [1, 100]
  • the two variables were generated and compared one million times
  • the tests were run 10 times

And here are the results:

 $a == $b $a === $b
--------- ---------
 0.765770  0.762020
 0.753041  0.825965
 0.770631  0.783696
 0.787824  0.781129
 0.757506  0.796142
 0.773537  0.796734
 0.768171  0.767894
 0.747850  0.777244
 0.836462  0.826406
 0.759361  0.773971
--------- ---------
 0.772015  0.789120

You can see that the speed is almost identical.


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