$test1 = (0 == "0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001");
$test2 = (0 == "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001");
var_dump($test1); //Returns False
var_dump($test2); //Returns True
我们正在使用PHP7,我们注意到了这一点。我们到了这一点,因为我们之前尝试了以下方法:
$test3 = (0 == "0"); //Returns true
$test4 = (0 == "1"); //Returns false
$test5 = (0 == null); //Returns true
因此,我们开始研究变量。
第一个问题是:为什么$test1
返回false
而$test2
返回true
?这与浮点精度有关吗?还是php.ini
中其他一些调整控制了string
的长度?
PD:虽然这不会影响我们的实际用例场景,但我们正在考虑强迫自己始终使用===
而不是==
来避免这种行为。