在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。使用 == 运算符是不是发生了类型转换?
但是为什么会相等……

解决方案 »

  1.   

    是的,php做了数据类型转换
    既然是比较大小,当然需要是同样的数据类型才能比较啦
      

  2.   

    灰常感谢……
    找到了官方的解释:
    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?>