php-为什么此评估为true

乳清这算是真的吗?

<?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几乎与此完全相同.

如果您不希望类型变戏法,请使用===而不是==.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...