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

Notes on using the PHP IN_ARRAY function

高洛峰
Release: 2023-03-03 22:14:02
Original
1274 people have browsed it

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



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template