开发者

identity conditional "===" , performance, and conversion

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::red开发者_StackOverflow中文版irect('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?


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.


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.


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.


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.


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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜