var_dump("10" == "10.0000"); var_dump("10" == "+10."); var_dump("10" == "10 "); #output bool(true) bool(true) bool(false)
更新
因此,baba在comparison involves numerical strings以下的回答确实帮助我了解了正在发生的事情.函数is_numeric将返回给您,无论字符串是否被视为数字字符串.有趣的是,“10”不是数字字符串,而是“10”.我挖掘了PHP源代码,我相信implementation of is_numeric在is_numeric_string_ex函数中.从那一点可以确切地知道PHP何时将字符串视为数字字符串.
解决方法
var_dump("10" == " 10"); // true
所以,如果你跑
var_dump("10" == "10 "); //false
你实际运行的是因为它将被视为一个字符串
var_dump("10" == 0); //false
这是因为Type juggling会将“10”转换为0,这是在PHP Documentation中
TRUE if $a is equal to $b after type juggling.
If you compare a number with a string or the comparison involves numerical strings,then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.
如果你想欺骗类型杂耍
var_dump("10" == 0 + "10 "); // true
An example of PHP’s automatic type conversion is the addition operator ‘+’. If either operand is a float,then both operands are evaluated as floats,and the result will be a float. Otherwise,the operands will be interpreted as integers,and the result will also be an integer. Note that this does not change the types of the operands themselves; the only change is in how the operands are evaluated and what the type of the expression itself is.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。