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

前端 未结 9 1789
佛祖请我去吃肉
佛祖请我去吃肉 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:07

    I think it is best to show by examples I did, while running into the same weird behavior. See my test case and hopefully it will help you understand the behavior better:

    // Normal comparison using the == Operator
    echo (0 == "0"); // true
    echo (0 == "a"); // true
    echo (0 == "safta!"); // true
    echo (1000 == "bla"); // false. It appears that PHP has a weird behavior only with the number / string 0 / "0" according to the past 3 examples.
    echo (23 == "23"); // true. So as we said, PHP has a problem (not a problem but weird behavior) only when the number / string 0 (or "0") is present
    echo (23 == "24"); // false. values aren't equal (unlike last example). The type is less relevant with the == operator as we can see.
    
    // Now using the === and !== Operators
    echo ("0" === 0); // false, since === requires both value and type to be the same. Here, type is different (int vs string)
    echo ("0" !== 0); // true because they aren't the same in terms of === comparison (type is different and that's why it's true)
    echo ("bla" === "blaa"); // false because the values are not the same. The type is the same, but === checks for both equal type and equal value.
    
    //Now using casting and === Operator:
    echo ((string)123 === "123"); // true. The casting of the int 123 to string changed it to "123" and now both variables have same value and are of same type
    echo ((int)"123" === 123); // true. The casting of the string 123 to int, changed it to int, and now both variables are of same value and type (which is exactly what the === operator is looking for)
    
    // Now using casting and == Operator. Basically, as we've seen above, the == care less for the
    // type of var, but more to the value. So the casting is less relevant here, because even
    // without casting, like we saw earlier, we can still compare string to int with the == operator
    // and if their value is same, we'll get true. Either way, we will show that:
    echo ((string)123 == "123"); // true. The casting of the int 123 to string changed it to "123" and now both vars have same value and are of same type
    echo ((int)"123" == 123); // true. The casting of the string 123 to int, changed it to int, and now both vars are of same value and type (which is exactly what the === operator is looking for)
    

提交回复
热议问题