in_array('01',array('1'))為什麼返回true

來源:互聯網
上載者:User
echo in_array('01',array('1'))

在使用in_array不採用strict時,為什麼返回1,怎麼判斷的?

回複內容:

echo in_array('01',array('1'))

在使用in_array不採用strict時,為什麼返回1,怎麼判斷的?

in_array($needle, $haystack) — 檢查數組中是否存在某個值

你可以理解為依次取數組中的值,然後跟 $needle 做比較。如果 == 判斷成立則返回 true.類似虛擬碼:

function in_array($needle, $haystack) {    foreach($haystack as $val) {        if ($val == $needle) {            return true;        }    }    return false;}

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

var_dump(("01" == 1));var_dump((" 1 " == 1));

然後你就明白了

補充一下:

就樓主問題來說, 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

/* {{{ 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);}/* }}} */

繼續搜尋 php_search_array 的源碼,當使用非strict 模式時,調用 fast_equal_check_function 函數,藉助 github 的搜尋功能,快速定位到 Zend/zend_operators.h 檔案

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;}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.