Home >Backend Development >PHP Tutorial >Some thoughts caused by php in_array function

Some thoughts caused by php in_array function

高洛峰
高洛峰Original
2016-11-29 11:47:331022browse

Today I suddenly thought that the in_array function in PHP has a strange usage. Let’s take a look at this usage. Friends in need can simply refer to it. The code is as follows:

$a=array("1,2,3" , "4");

if(in_array(1,$a)){

echo "The condition is established";

}

This condition is indeed established. According to my thinking logic, the above condition This is only true when the value of parameter 1 is "1, 2, 3" and the sum is 4, but strangely it is true. Battlefield immediately tested the following situation, the code is as follows:

$a=array("a ,2,3","4");

if(in_array('a',$a)){

echo "The condition is not established";

}

This is not true, In the first test case, if parameter 1 is written as '1', the condition is not established. The conclusion is:

In PHP, if the first parameter is a number - a number without quotation marks, the second If an array parameter contains a string starting with that number, it will return a true value regardless of whether the first parameter is a complete element of the second parameter. In other words, the in_array function does not always follow my thinking logic. Does the entire element match, or do I not understand the data type clearly enough?

At the same time, I also found that if the first element of your array is 0, problems will also occur. See the following situation. The code is as follows:

$a=array(0, "m");

if(in_array('mc6',$a))

{

echo "=====";

}//Open source code phpfensi.com


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