Home > Backend Development > PHP Tutorial > in_array('01',array('1'))为什么返回true

in_array('01',array('1'))为什么返回true

WBOY
Release: 2016-06-06 20:41:48
Original
1406 people have browsed it

<code>echo in_array('01',array('1'))
</code>
Copy after login
Copy after login

在使用in_array不采用strict时,为什么返回1,怎么判断的?

回复内容:

<code>echo in_array('01',array('1'))
</code>
Copy after login
Copy after login

在使用in_array不采用strict时,为什么返回1,怎么判断的?

in_array($needle, $haystack) — 检查数组中是否存在某个值

你可以理解为依次取数组中的值,然后跟 $needle 做比较。如果 == 判断成立则返回 true.类似伪代码:

<code>function in_array($needle, $haystack) {
    foreach($haystack as $val) {
        if ($val == $needle) {
            return true;
        }
    }
    return false;
}
</code>
Copy after login

注意:在 PHP 中 == 同 === 的差别。

<code>var_dump(("01" == 1));

var_dump((" 1 " == 1));
</code>
Copy after login

然后你就明白了

补充一下:

就楼主问题来说, in_array 判断 字符串和数字是否相等时对输入的字符串 “01” 进行了取值。详细情况可以参见: PHP类型比较表 - PHP官方网站 php.net

  • '01' == '1'; 结果是 TRUE
  • echo TRUE 结果是 1

再附一个《PHP 类型比较表》http://php.net/manual/zh/types.comparisons.php


in_array 源码定义在 https://github.com/php/php-src/blob/master/ext/standard/array.c

<code>/* {{{ proto bool in_array(mixed needle, array haystack [, bool strict])
   Checks if the given value exists in the array */
PHP_FUNCTION(in_array)
{
    php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
</code>
Copy after login

继续搜索 php_search_array 的源码,当使用非严格模式时,调用 fast_equal_check_function 函数,借助 github 的搜索功能,快速定位到 Zend/zend_operators.h 文件

<code>static zend_always_inline int fast_equal_check_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
{
    if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
        if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
            return Z_LVAL_P(op1) == Z_LVAL_P(op2);
        } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
            return ((double)Z_LVAL_P(op1)) == Z_DVAL_P(op2);
        }
    } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
        if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
            return Z_DVAL_P(op1) == Z_DVAL_P(op2);
        } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
            return Z_DVAL_P(op1) == ((double)Z_LVAL_P(op2));
        }
    } else if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
        if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
            if (Z_STR_P(op1) == Z_STR_P(op2)) {
                return 1;
            } else if (Z_STRVAL_P(op1)[0] > '9' || Z_STRVAL_P(op2)[0] > '9') {
                if (Z_STRLEN_P(op1) != Z_STRLEN_P(op2)) {
                    return 0;
                } else {
                    return memcmp(Z_STRVAL_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op1)) == 0;
                }
            } else {
                zendi_smart_strcmp(result, op1, op2);
                return Z_LVAL_P(result) == 0;
            }
        }
    }
    compare_function(result, op1, op2 TSRMLS_CC);
    return Z_LVAL_P(result) == 0;
}
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template