Today it suddenly occurred to me that the in_array function in PHP has a strange usage. Let's take a look at this usage for a brief reference by friends in need.
代码如下 | 复制代码 |
$a=array("1,2,3","4"); if(in_array(1,$a)){ echo "条件成立"; } |
This condition is indeed true. According to my thinking logic, the above condition is only true when the value of parameter 1 is "1, 2, 3" and the sum is 4, but the strange thing is that it is true. Battlefield then tested the following scenarios:
代码如下 | 复制代码 |
$a=array("a,2,3","4"); if(in_array('a',$a)){ echo "条件不成立"; } |
This is not true. In the first test case, if parameter 1 is written as '1', the condition is also not true. Battlefield concluded:
In PHP, if the first parameter is a number - an unquoted number, and the second array parameter contains a string starting with that number, it does not matter whether the first parameter is the complete second parameter. elements, all return true values. In other words, the in_array function does not match the entire element according to my thinking logic every time, or do I not understand the data type clearly enough?
At the same time, Battlefield also discovered that if the first element of your array is 0, problems will also occur. See the following situation
The code is as follows | Copy code | ||||
if(in_array('mc6',$a)) { echo "====="; } |