乳清这算是真的吗?
<?PHP
$val2=0;
//outputs that is an error123
if($val2=='error123'){
echo 'that is an error123<br />';
}else{
echo 'that is not an error123<br />';
}
解决方法:
您正在将字符串与整数进行比较.为了进行比较,首先将字符串转换为整数.当“ error123”转换为整数时,它变为0.
echo intval("error123");
结果:
0
在PHP手册中,此行为有an explanation.
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.
有一个快速参考页PHP type comparison tables,向您显示各种比较的结果.请参阅表格“与==的宽松比较”.关于这个问题,有趣的部分是0 ==“ PHP”显示为TRUE.
在type juggling上还有一个页面.该页面上的user comment几乎与此完全相同.
如果您不希望类型变戏法,请使用===而不是==.