if(0 == '%') echo “WTF, Php, why can't you compare things sanely?”

后端 未结 5 767
灰色年华
灰色年华 2021-01-13 03:31

I just reduced a crazy bug to what\'s in the title of this question. I\'m sure there\'s a Perfectly Reasonable Explanation for why Php thinks that 0 == \'%\' (or any other s

5条回答
  •  花落未央
    2021-01-13 04:20

    In the event that a strict comparison operator is not directly suitable, if you want both values to be compared as strings, you can use strcmp(). An example could be where both values are variables, and the types might be either string, or int. Instead of type casting to string and then using strict equality check, strcmp() can be less verbose.

    php's type coercion is very convenient. But if you don't understand its many rules(some can bite), you should try to avoid using it. See http://www.php.net/manual/en/types.comparisons.php

提交回复
热议问题