PHP 中的 == 和 === 运算符
假设我有一个始终是字符串的变量。
现在看下面的代码:
if($myVar === "teststring")
注意: $myVar
将始终是一个字符串,所以我的问题是
哪个更快/最好,使用 ===
(缩进)或 < code>== (相等)?
Let's 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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
身份测试总是更快,因为 PHP 不需要 Type Juggle 来评估比较。然而,我想说速度差异在纳秒范围内并且完全可以忽略不计。
相关阅读:
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:
===
会稍微快一点,但更重要的是,它强制$myVar
will 是一个字符串,所以你不必担心关于它是其他类型可能产生的影响。===
will be slightly faster, but more importantly, It enforces that$myVar
will be a string so you don't have to worry about the possible effects of it being some other type.一般来说,当我编码时,我使用 == 而不是 ===,但是,使用恒等式更精确,而且速度稍快(差异很小)。
两者之间的差异可能与您的需求无关。
In general when I code, I use == over ===, however, using the identity is more precise, and also, slightly faster (difference is minimal).
The difference between the two is likely irrelevant for whatever you need.