English 中文(简体)
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::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?

最佳回答

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 




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签