In_array (' "", Array (' 1 ')) Why return True

Source: Internet
Author: User
Tags comparison table
echo in_array('01',array('1'))

When using In_array not to use strict, why return 1, how to judge?

Reply content:

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

When using In_array not to use strict, why return 1, how to judge?

In_array ($needle, $haystack)-checks if a value exists in the array

You can understand that by taking the values in the array sequentially, and then comparing them with the $needle. Returns true if = = is determined. Pseudo-code like:

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

Note: the difference between = = = = = = In PHP.

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

And then you get it.

Add:

In the case of landlord problem, in_array the input string "01" When judging whether the string and the number are equal. Details can be found in: PHP type comparison table-PHP official website php.net

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

Attach a "PHP type comparison table" http://php.net/manual/zh/types.comparisons.php

in_arrayThe source code is defined 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 to search php_search_array the source code, when using non-strict mode, call the fast_equal_check_function function, with the help of GitHub search function, quickly navigate to 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_T Ype_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        RN 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)) {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;}
  • Related Article

    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.