比较,!== 与 !=

发布于 2024-11-15 07:22:40 字数 241 浏览 2 评论 0原文

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

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

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

他们这样做有什么原因吗? !==!= 快还是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

愚人国度 2024-11-22 07:22:41

!= 运算符有点,因为它包含必要的隐式转换,

!== 则比较前面的类型,后面的值(仅当类型相等时)

此外,遵循 此答案有关差异的更多信息两个操作数之间

The != operator is a bit slower because it includes an implicit conversion if necessary,

while !== compares the types before and then the values (only if types are equals)

Also, follow this answer for further infos on the differences between the two operands

伴我心暖 2024-11-22 07:22:41

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

但是,如果您确实有要求,并且参数应该完全符合您的预期(例如,当您不希望 0 传递等式时,=== false test),然后使用 ===!==

正如 Headshota 所提到的,如果您正在使用对象,则 === 检查两个变量是否实际上指向同一个对象实例,而 ==< /code> 尝试按值匹配它们,这可能会导致意外结果,应谨慎使用。这里将使用严格运算符,因为它对应用程序有意义。

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

当需要这样做时,必须使用严格运算符,而不是作为性能优化。

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

$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; 强制隐式转换,我得到粗略:

4.4 seconds for ==
2.4 seconds for ===

这意味着在这两种情况下大约都有 2 秒的增益。您可能会认为(在某种程度上您是对的),这证明了严格比较运算符的速度优势。但不要忘记我们正在谈论 1e6 * 100 = 1 亿次迭代。

这意味着一次严格比较将使您的脚本速度加快 0.00000002 秒

如果您认为这是编写脚本时应该考虑的事情,那么就这样做。但您很快就会遇到其他微小的优化,这些优化将为您提供 1000 倍的速度增益,并使此类“优化”看起来毫无用处。这是过早优化的教科书案例。

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

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.

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 !==.

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.

The strict operators have to be used when it means something to do so, not as a performance optimization.

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);

I roughly get:

6.6 seconds for ==
4.3 seconds for ===

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

4.4 seconds for ==
2.4 seconds for ===

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.

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

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.

携君以终年 2024-11-22 07:22:41
For example

$flag = 0;
if($flag == false ){
   echo "true";
}else{
   echo "false";
}                      // echo true;


$flag = 0;
if($flag === false ){
   echo "true";
}else{
   echo "false";
}                     // echo false;


$flag = false;
if($flag === false ){
   echo "true";
}else{
   echo "false";
}                    // echo true
For example

$flag = 0;
if($flag == false ){
   echo "true";
}else{
   echo "false";
}                      // echo true;


$flag = 0;
if($flag === false ){
   echo "true";
}else{
   echo "false";
}                     // echo false;


$flag = false;
if($flag === false ){
   echo "true";
}else{
   echo "false";
}                    // echo true
渡你暖光 2024-11-22 07:22:41

实际上,!== 不是用来比较变量类型的,它是用来确保比较的值是相同的类型以及具有相同的值。

因此,如果您有

$i = "0";

$i == 0 将返回 true,而 $i === 0 将返回 false。

如果您有

$j = 0;

$j == null 将返回 true,而 $j === null 将返回 false。

Actually, !== is not used to compare variable types, it is used to ensure that the values being compared are the same type as well as have the same value.

So if you have

$i = "0";

$i == 0 will return true, while $i === 0 will return false.

if you have

$j = 0;

$j == null will return true, while $j === null will return false.

何必那么矫情 2024-11-22 07:22:41

当您知道类型相同时,它并没有真正更快,这就是您的示例中的情况。人们使用 !== 的主要原因是它阻止 PHP 执行其 类型杂耍魔法,因此比较意味着您不想要的内容的可能性较小。

It's not really faster when you know the type is the same, which is the case in your example. The major reason people use !== is that it keeps PHP from doing its type-juggling magic, so there's less chance of the comparison meaning something you didn't want it to.

清风不识月 2024-11-22 07:22:41

检查同一性通常比检查相等性更安全,这可以避免由无效类型引起的许多错误。但在某些情况下,身份不是必须的,因此在这种情况下您可以使用相等性。

It is generally safer to check identity, than equality, this avoids many errors caused by invalid types. but there are cases when identity is not a must, so you can use equality in this cases.

乱世争霸 2024-11-22 07:22:40

!= 只比较值是错误的。因为它比较类型,所以应用 类型转换 如有必要(请参阅表< em>比较与各种类型),然后比较(转换后的)值。

与此相反,如果类型不相等且未完成类型转换,则 !== 会更早失败。

It is wrong to say that != does only compare the value. Because it rather compares the types, applies a type conversion if necessary (see table of Comparison with Various Types) and then compares the (converted) values.

In contrast to that, !== fails earlier if the types are not equal and no type conversion is done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文