Why does PHP consider 0 to be equal to a string?

前端 未结 9 1802
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 02:41

I have the following piece of code:

$item[\'price\'] = 0;
/* Code to get item information goes in here */
if($item[\'price\'] == \'e\') {
    $item[\'price\'         


        
9条回答
  •  悲哀的现实
    2020-11-22 03:17

    "ABC" == 0
    

    evaluates true because first "ABC" is converted to integer and becomes 0 then it is compared to 0.

    This is an odd behaviour of the PHP language: normally one would expect 0 to be promoted to string "0" and then compared to "ABC" with a result false. Perhaps that's what happen in other languages like JavaScript where the weak comparison "ABC" == 0 evaluates false.

    Doing a strict comparison solves the problem:

    "ABC" === 0
    

    evaluates false.

    But what if I do need to compare numbers as strings with numbers?

    "123" === 123
    

    evaluates false because the left and right term are of different type.

    What is actually needed is a weak comparison without the pitfalls of PHP type juggling.

    The solution is to explicit promote the terms to string and then do a comparison (strict or weak doesn't matter anymore).

    (string)"123" === (string)123
    

    is

    true

    while

    (string)"123" === (string)0
    

    is

    false


    Applied to the original code:

    $item['price'] = 0;
    /*code to get item information goes in here*/
    if((string)$item['price'] == 'e') {
        $item['price'] = -1;
    }
    

提交回复
热议问题