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

后端 未结 5 768
灰色年华
灰色年华 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:03

    This is because it converts '%' to number and it is 0.

    The way, however, how PHP converts strings to numbers is pure evil IMHO, because non number string does not have to result in zero.

    If it starts with a digit then php will result the first number it can find in it and ignore the rest of the string. So:

    "2 times 5 equals 10"
    

    ..will result in 2 when casted to a number.

    Article in the documentation

提交回复
热议问题