Home  >  Article  >  php教程  >  PHP字符串 ==比较运算符的副作用

PHP字符串 ==比较运算符的副作用

WBOY
WBOYOriginal
2016-06-13 12:21:05757browse

复制代码 代码如下:


$a = '212345678912000005';
$b = '212345678912000001';
var_dump($a == $b);


这段代码的输出是bool(true), 说明这样判断会得出结论是两者相等. 类似的特性在in_array()函数第3个参数为false或者不设置的情况. 原因是首先判断字符串是否是数字, 然后转换成long或者double(C语言数据类型)再判断 - 使用zendi_smart_strcmp. 但是, 源码中的注释说声明考虑了溢出的情况,

复制代码 代码如下:


} else if (dval1 == dval2 && !zend_finite(dval1)) {
    /* Both values overflowed and have the same sign,
     * so a numeric comparison would be inaccurate */
    goto string_cmp;
}


dval1和dval2分别是两个字符串转换为double型后的值. 但为什么还是这样呢? 也许这个判断并不正确. 进一步研究再说...

解决方法, 使用三个等号"==="代替两个等号"==", in_array()函数设置第三个参数为true: in_array('val', $array, true).
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn