为什么整数0等于PHP中的字符串? [英] Why is integer 0 equal to a string in PHP?
本文介绍了为什么整数0等于PHP中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么
var_dump(0 ==string);
输出
bool(true)
不是 ==
运算符应该将 0
转换为 FALSE
和 string 转换为
TRUE
p>
var_dump(0 ==string);
正在进行数字(整数)比较
0是一个整数,因此string被转换为整数以进行比较,并且等于整数值0,因此0 == 0为真
有关详细信息,请参阅PHP文档中的与各种类型表格的比较。
Possible Duplicate:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?
Why this
var_dump(0 == "string");
outputs this
bool(true)
Isn't the context of ==
operator supposed to convert 0
into FALSE
and "string"
into TRUE
according to this set of rules?
解决方案
var_dump(0 == "string");
is doing a numeric (integer) comparison
0 is an integer, so "string" is converted to an integer to do the comparison, and equates to an integer value of 0, so 0 == 0 is true
Se the comparison with various types table in the PHP documentation for details
这篇关于为什么整数0等于PHP中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文