Home  >  Article  >  Backend Development  >  Notes on using the PHP IN_ARRAY function_PHP tutorial

Notes on using the PHP IN_ARRAY function_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:35:56909browse

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

Let me 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 a number type, and 's' is a string type. According to the instructions in the "Comparison Operators" chapter in the php manual, number and string are compared

At this time, 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 in_array(0, array('s', The result of 'ss')) is also true

If the third parameter strict of in_array is set to true, it will be judged whether the values ​​and types are equivalent during comparison. If they are equivalent, true will be returned. , otherwise it returns false.

This is just for note.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322204.htmlTechArticleIn addition, when we compare whether two values ​​​​are consistent in the control structure, we should try to use === instead. == (Of course, this is also more appropriate according to the specific business logic)...
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