php为什么0 =='somestr'评估为true

Mub*_*bas 0 php

我有这个代码:

echo ( $arr['process_refunds'] == 'storecredit' ) ? 'true' : 'false'
Run Code Online (Sandbox Code Playgroud)

$arr['process_refunds']0类型int(0).

它尝试了php -a,结果是如果我们比较任何字符串 ==int(0)它评估true.

这是为什么?

I_A*_*LES 5

这是因为==是一个松散类型的比较运算符.它将您的字符串转换为数字,因此它的计算结果为true.尝试使用===.

您的字符串将转换为零,因为它以字母开头.