Home  >  Article  >  Backend Development  >  Notes on using the PHP IN_ARRAY function

Notes on using the PHP IN_ARRAY function

高洛峰
高洛峰Original
2016-12-22 13:45:471172browse

In addition, when we control the structure to compare whether two values ​​​​are consistent, we should try to use === instead of == (of course, this should also be selected according to the specific business logic).

Explain why
var_dump(in_array(0, array('s' ));

The result of this sentence is bool(true).

Because in_array will compare 0 and 's', 0 is number Type, 's' is a string type. According to the description in the "Comparison Operators" chapter in the php manual, when number and string are compared, the string type will be converted to number first, and then the comparison operation will be performed. The result of converting s' to number is 0, and the result of 0 == 0 is true, so the result of in_array(0, array('s', 'ss')) is also true

If the third parameter of in_array is If strict is set to true, it will be judged whether the values ​​and types are equal during comparison. If they are equal, true will be returned, otherwise false will be returned. Please pay attention to PHP Chinese website



Statement:
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