In_array ('01', array ('1') Why does return true?

Source: Internet
Author: User
{Code...} When in_array is used and strict is not used, why does 1 be returned? How can this problem be determined?
echo in_array('01',array('1'))

When in_array is used instead of strict, why does 1 be returned? How can this problem be determined?

Reply content:
echo in_array('01',array('1'))

When in_array is used instead of strict, why does 1 be returned? How can this problem be determined?

In_array ($ needle, $ haystack)-check whether a value exists in the array.

You can take the values in the array in sequence and compare them with $ needle. True is returned if = is true. It is similar to pseudo code:

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

Note: The difference between = and = in PHP.

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

Then you will understand

Add:

For the landlord's question, when in_array determines whether the string and number are equal, it takes the value of the input string "01. For details, see PHP type comparison table-PHP official website php.net

  • '01' == '1';The result is TRUE.
  • echo TRUEThe result is 1.

A PHP type comparison table http://php.net/manual/zh/types.comparisons.php is attached.

in_arraySource Code definition in 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);}/* }}} */

Continue searchphp_search_arrayWhen the non-strict mode is usedfast_equal_check_functionFunction. With the help of github's search function, you can quickly find the Zend/zend_operators.h file.

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.