The PHP IN_ARRAY function USES considerations

  • 2020-03-31 21:04:09
  • OfStack

In addition, we should try to use === instead of == when comparing two values in the control structure. (of course, this is also appropriate according to the specific business logic).

Little brother to explain why
Var_dump (in_array (0, array (' s'));

The result of this sentence is a bool(true).

Because in_array compares 0 to 's', which is of type number and 's' of type string, according to the "Comparison Operators" chapter in PHP manual, number and string

When you compare, you first convert the string type to number, and then you compare. 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 set strictly to true, the comparison will determine whether the value and type are equivalent. Returns true if both are equal, or false if not.

Just remember.

Related articles: