Today, I suddenly thought that the in_array function in php has a strange usage. Let's take a look at this usage. A friend in need can give a simple reference.
The Code is as follows: |
Copy code |
$ A = array ("1, 2, 3", "4 "); If (in_array (1, $ )){ Echo "condition is true "; } |
This condition is true. According to my thinking logic, the condition above is true only when the value of parameter 1 is "1, 2, 3" and 4, but the strange thing is that he was founded. The battlefield immediately tested the following situations:
The Code is as follows: |
Copy code |
$ A = array ("a, 2, 3", "4 "); If (in_array ('A', $ )){ Echo "the condition is not true "; } |
This is not true. In the first test case, if parameter 1 is written as '1', it is not true. The battlefield concluded that:
In PHP, if the first parameter is a number-A number without quotation marks, if the second array parameter contains a string starting with this number, the true value is returned regardless of whether the first parameter is a complete element of the second parameter. That is to say, not every time the in_array function matches the entire element according to my thinking logic, or is my understanding of the Data Type unclear?
At the same time, the battlefield also finds that if the first element of your array is 0, there will also be problems, as shown below:
The Code is as follows: |
Copy code |
$ A = array (0, "m "); If (in_array ('mc6', $ )) { Echo "= "; } |