在php官方的手册当中提供了这样一个例子:Beware of certain control behavior with boolean and non boolean values :<?php
// Consider that the 0 could by any parameters including itself
var_dump(0 == 1); // false
var_dump(0 == (bool)'all'); // false
var_dump(0 == 'all'); // TRUE, take care
var_dump(0 === 'all'); // false// To avoid this behavior, you need to cast your parameter as string like that :
var_dump((string)0 == 'all'); // false
?>
不解为什么var_dump(0 == 'all'); 为true。使用 == 运算符是不是发生了类型转换?
但是为什么会相等……
// Consider that the 0 could by any parameters including itself
var_dump(0 == 1); // false
var_dump(0 == (bool)'all'); // false
var_dump(0 == 'all'); // TRUE, take care
var_dump(0 === 'all'); // false// To avoid this behavior, you need to cast your parameter as string like that :
var_dump((string)0 == 'all'); // false
?>
不解为什么var_dump(0 == 'all'); 为true。使用 == 运算符是不是发生了类型转换?
但是为什么会相等……
解决方案 »
- 团购网站信息收录
- 新闻的html格式,转换为xml
- 网站文件扩展名问题.郁闷中
- PHP数组,$a = {0,1,2,3}怎样变成$a = {1,2,3,4]
- 拖动网页的滚动条,网页的开头还是在那个位置
- 通过form表单字段fstr传过来一个字符串$str,在处理页面中赋值$str=123,希望print $fstr的结果是123,该怎么实现?谢谢!
- 我有个外包项目,谁有兴趣?(限上海地区)
- 谁能给一个nuSoap在php环境搭建WebService的大致思路?
- 在一个页面中多次使用include会增大服务器的压力吗?
- 关于微信扫描支付的问题 PHP版
- 不同的子类是否可以继承同一个父类
- 把curl返回的cookies处理成数组
既然是比较大小,当然需要是同样的数据类型才能比较啦
找到了官方的解释:
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. <?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true?>