Lets say I have a variable that will always be a string.

Now take the code below:

if($myVar === "teststring")

Note $myVar will always be a string, so my questions is

Which is quicker/best, using === (Indentity) or the == (Equality)?


I would use == in that case since you don't really care about the type of $myVar. I'm pretty sure speeds are about the same (it's so trivial that it dosen't matter - even in a mega loop).

Written by AlexV

Accepted Answer

Testing for identity is always faster, because PHP does not have to Type Juggle to evaluate the comparison. However, I'd say the speed difference is in the realms of nanoseconds and totally neglectable.

Related reading:

Written by Gordon
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki