比较,!==与!= [英] comparing, !== versus !=

查看:192
本文介绍了比较,!==与!=的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道!== 也用于比较变量类型,而!= 只比较值。

I know that !== is used to compare variable types too, while != only compares values.

但我看到许多人在比较值时使用!== ,例如:

But I see that many people use !== when they compare values, for example:

$stuff = 'foo';
if($stuff !== 'foo') // do...

他们有什么原因吗?

Is there any reason they do this? Is !== faster than != or what?

推荐答案

如果你事先知道这两个变量是相同的类型,那么它不会有任何区别。我认为速度差异是可以忽略的,速度的参数可以(应该)完全被忽略,你应该专注于你的应用程序,而不是专注于过早(和不必要的)优化。

If you do know in advance that both variables are of the same type, then it won't make any difference. I think the speed difference is just so negligible that the speed argument can (and should) be completely ignored, and you should focus on your application rather than focusing on premature (and unnecessary) optimization.

然而,如果你有一个要求,参数应该是你期望的(例如, === false 当你不想要 0 以传递等式测试),然后使用 === !==

However, if you do have a requirement that and argument should be exactly what you expect (for example, === false when you don't want 0 to pass the equality test), then use === or !==.

正如Headshota所提到的,如果你使用对象,那么 === 检查两个变量实际上是否指向相同的对象实例,而 == 尝试通过值匹配它们,这可能导致意外的结果,应该非常小心使用。

As mentioned by Headshota, if you are working with objects, then === checks that the two variables actually point to the same object instance, whereas == tries to match them by value, which might lead to unexpected results and should be used with great care. Here the strict operator will be used because it means something to the application.

要回答你的问题,如果在你的特定上下文中有人使用 === !== 你知道两个变量是相同的(标量)类型,那么我想这只是一个事的代码风格,并在代码中显示出一点严格性,强制两个变量是相同类型的想法,可能不考虑性能。

To answer your question, if in your particular context some people are using === or !== where you do know that both variables are of the same (scalar) type, then I guess it's just a matter of coding style, and shows a bit of strictness in the code, enforcing the idea that both variables are of the same type, probably not with performance in mind.

严格

更新 回复速度参数,让我们考虑这个小基准:

Update: to reply to the speed argument, let's consider this small benchmark:

$a = "test";
$b = "test";

$t = microtime(true);
for ($i=0; $i<1e6; $i++) {
    ($a == $b);
    // (100x the equality test, omitted for clarity purposes,
    //  to lower the speed impact of the loop itself)
    ($a == $b);
}
printf('%.3f', microtime(true)-$t);

我粗略地得到:

6.6 seconds for ==
4.3 seconds for ===

现在,如果我使用 $ b = 1; 强制隐式转换,我大致:

Now if I use $b = 1; to force an implicit conversion, I get roughly:

4.4 seconds for ==
2.4 seconds for ===

这意味着在这两种情况下大约增加2秒。你可能会想,你会在某种程度上是正确的,这是一个严格的比较运算符的速度优势的演示。但不要忘记,我们正在谈论 1e6 * 100 = 1亿次迭代。

This means roughly a 2 seconds gain in both cases. You might think, and you will be right in some way, that this is a demonstration of the speed advantage of the strict comparison operator. But don't forget that we are talking about 1e6 * 100 = 100 million iterations.

这意味着单个严格比较将加速你的脚本 0.00000002秒

That means that a single strict comparison will speed up your script by 0.00000002 seconds.

如果你认为这是你应该考虑的当写你的脚本,然后这样做。但你很快会遇到其他微小的优化,将给你1000倍的速度增益,使这样的优化看起来没有用。这是一个过早优化的教科书案例。

If you think that this is something you should consider when writing your script, then do so. But you'll soon encounter other tiny optimizations that will give you 1000 times this speed gain, and make such "optimizations" look useless. This is a textbook case of premature optimization.

同样,如果你必须使用严格的比较运算符,因为它是你的代码中的一个要求。 出于性能原因不要使用

Again, use the strict comparison operators if you have to, because it is a requirement in your code. Do not use it for performance reasons.

这篇关于比较,!==与!=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆