身份条件“====” 、性能和转换

发布于 2024-11-04 15:53:46 字数 974 浏览 1 评论 0原文

我总是远离 stackoverflow 的答案,并且我读过的任何文章都认为 "===" 优于 "==" 因为使用了更严格的比较,并且您不会为了检查匹配而浪费资源转换值类型。

我可能会用错误的假设来解决这个问题,所以我假设这个问题的一部分是“我的假设是真的吗?”

其次,

我正在专门处理一种情况,即我以字符串“100”的形式从数据库获取数据。

我正在比较的代码是这样的...

    if ($this->the_user->group == 100) //admin
    {
        Response::redirect('admin/home');
    }
    else   // other
    {
        Response::redirect('user/home');
    }

    if ( (int) $this->the_user->group === 100) //admin
    {
        Response::redirect('admin/home');
    }
    else   // other
    {
        Response::redirect('user/home');
    }

甚至

    if (intval($this->the_user->group) === 100) //admin
    {
        Response::redirect('admin/home');
    }
    else   // other
    {
        Response::redirect('user/home');
    }

是通过手动转换或简单转换获得的任何完整性(或性能),以便您可以使用身份('===')比较?

I've always came away from stackoverflow answers and any reading I've done that "===" is superior to "==" because uses a more strict comparison, and you do not waste resources converting value types in order to check for a match.

I may be coming at this with the wrong assumption, so I assume part of this question is, "is my assumption true?"

Secondly,

I'm dealing specifically with a situation where I'm getting data from a database in the form of a string "100".

The code I am comparing is this...

    if ($this->the_user->group == 100) //admin
    {
        Response::redirect('admin/home');
    }
    else   // other
    {
        Response::redirect('user/home');
    }

vs.

    if ( (int) $this->the_user->group === 100) //admin
    {
        Response::redirect('admin/home');
    }
    else   // other
    {
        Response::redirect('user/home');
    }

or even

    if (intval($this->the_user->group) === 100) //admin
    {
        Response::redirect('admin/home');
    }
    else   // other
    {
        Response::redirect('user/home');
    }

is any integrity (or performance) gained by manually casting or converting simply so you can use the identity ('===') comparison?

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

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

发布评论

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

评论(5

初见终念 2024-11-11 15:53:46

在您的特定情况下 == 是更好的选择。正如您(如代码中所示)可能已经发现,即使您获取整数,许多数据库函数也将始终返回字符串。因此,输入严格比较实际上只会使您的代码变得臃肿。

此外,您还增加了潜在的(我们称之为理论上的)安全风险。例如,(int) '100AB2' 将产生 100。在您的情况下,这可能不会发生,但在其他情况下可能会发生。

所以:不要过度使用严格比较,它并不总是好的。您主要只在不明确的情况下才需要它,例如 strpos 的返回值。

In your particular case == is the better option. As you (as can be seen in your code) have probably already found out many database functions will always return strings, even if you fetch an integer. So type strict comparison really only bloats your code.

Furthermore you are adding a potential (let's call it theoretic) security risk. E.g. (int) '100AB2' would yield 100. In your case this probably can't happen, but in others it may.

So: Don't overuse strict comparison, it's not always good. You mainly need it only in ambiguous cases, like the return value of strpos.

月朦胧 2024-11-11 15:53:46

===== 之间存在性能差异 - 后者甚至会快两倍,请参阅 相等与相同比较运算符
然而,差异太小,无需担心 - 除非代码被执行数百万次。

There is a performance difference between == and === - latter will be even twice as fast, see Equal vs identical comparison operator.
The difference, however is too small to be bothered with - unless the code is executed millions of times.

青丝拂面 2024-11-11 15:53:46

这是你正在做的一个非常小的优化。就我个人而言,我认为这并不值得。

当您显式转换该值时,使用 === 时不转换该值所获得的任何提升都会丢失。就您而言,由于类型对您来说并不重要,因此您应该只执行 == 并完成它。

我的建议是当您还需要检查类型时保留 === - 例如 0 评估为 false 等等。

That's a really tiny optimization you're doing there. Personally, I don't think it's really worth it.

Any boost you gain from not casting the value when using === is lost when you explicitly cast the value. In your case, since the type is not important to you, you should just do == and be done with it.

My recommendation would be to keep === for when you need to check type as well - e.g. 0 evaluating to false and so on.

街角迷惘 2024-11-11 15:53:46

任何性能提升在微观上都是很小的,除非您连续几天/几个月/几年执行数十亿和数万亿次这样的比较。严格比较确实有其用处,但它在 PHP 中也有些反常。 PHP 是一种弱类型语言,并且(通常)会做正确的事情,使自动转换/转换值成为正确的事情。大多数时候,没有必要进行严格的比较,因为 PHP 会做正确的事情。

但在某些情况下,例如使用 strpos 时,自动转换会失败。如果您正在搜索的针正好位于干草堆的开头,则 strpos 将返回“0”,这将被视为 FALSE,这是错误的。处理这个问题的唯一方法是通过严格比较。

Any performance gains will be microscopically small, unless you're performing literally billions and trillions of these comparisons for days/months/years on-end. The strict comparison does have its uses, but it also is somewhat of anomally in PHP. PHP's a weakly typed language, and (usually) does the right thing for auto-converting/casting values to be the right thing. Most times, it's not necessary to do a strict comparison, as PHP will do the right thing.

But there are cases, such as when using strpos, where the auto-conversion will fail. strpos will return '0' if the needle you're searching is right at the start of the haystack, which would get treated as FALSE, which is wrong. The only way to handle this is via the strict comparison.

二货你真萌 2024-11-11 15:53:46

PHP 有一些 WTF 松散比较返回 TRUE,例如:

array() == NULL

0 == 'Non-numeric string'

始终在变量和字符串之间使用严格比较

$var === 'string'

PHP has some WTF loose comparisons that return TRUE like:

array() == NULL

0 == 'Non-numeric string'

Always use strict comparison between a variable and a string

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