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

前端 未结 12 804
有刺的猬
有刺的猬 2020-12-12 10:39

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

相关标签:
12条回答
  • 2020-12-12 11:30

    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.

    0 讨论(0)
  • 2020-12-12 11:30

    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.

    0 讨论(0)
  • 2020-12-12 11:31

    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

    0 讨论(0)
  • 2020-12-12 11:33

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

    0 讨论(0)
  • 2020-12-12 11:35

    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.

    0 讨论(0)
  • 2020-12-12 11:35

    Faster should not just be measured in direct execution time (direct performance tests are almost negligible in this case). That said, I would need to see a test involving iteration, or recursion, to really see if there is a significant, cumulative difference (when used in a realistic context). The testing and debugging time you will save when dealing with edge cases should be meaningful to you, also

    0 讨论(0)
提交回复
热议问题