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 TRUE
The result is 1.
Attach a "PHP type comparison table" http://php.net/manual/zh/types.comparisons.php
in_array
The 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;}