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, take care
感到比较疑惑,其实原因是因为php在使用==
进行比较时,会先将all
尝试着转换为数字,这里因为里面并没有包含有效的数字,因此转换得到结果就是0,于是结果为true。
转载自:
网友评论