字符串会被转化为0,请看以下说明:
If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. These rules also apply to the switch statement.
参见[url]http://cn2.php.net/manual/en/language.operators.comparison.php[/url]
If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. 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("1" == "1e0"); // 1 == 1 -> true
switch ("a") {
case 0:
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}
?>参见[url]http://cn2.php.net/manual/en/language.operators.comparison.php[/url]
字符串转数字比较解析
本文解析了PHP中字符串与数字进行比较时的转化规则。当字符串与整数进行比较时,字符串会被转换为数字;若两个数值型字符串比较,则按整数比较。文章通过实例展示了这些规则,并解释了switch语句中的应用。
1355

被折叠的 条评论
为什么被折叠?



