$a='ten';
if($a == 0) {
echo 'equal 0';
} else {
echo 'not equal 0';
}
Now guess echo what?
yes ,of course echo 'equal 0 '
But why?
When use '==', the string $a will be change into a int ,and if $a is not started with char[_/d],it will be 0,so 0==0 is the truth.
We can use '===' to judge this .
本文通过一个简单的PHP示例展示了使用双等号(==)进行字符串与整数比较时可能遇到的问题。当字符串开头不是数字时,该字符串会被解释为0,从而导致意外的比较结果。为了避免这类问题,建议使用恒等运算符(===)来进行类型和值的同时比较。
1310

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



