Home >Backend Development >PHP Tutorial >Usage of in_array function in php

Usage of in_array function in php

怪我咯
怪我咯Original
2017-07-16 14:49:441935browse

in_array() Function Search for the specified value in the array. Returns TRUE if the value is found in the array, FALSE otherwise.

This article mainly introduces the usage of in_array function in php, and conducts a more in-depth exploration of the usage of in_arrayfunction parameter matching. It helps to understand the usage of the in_array function more comprehensively. Friends who need it can refer to

$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 when the value of parameter 1 is "1 , 2, 3" is only established when the sum is 4, but the strange thing is that it is actually established, and then the following situation is tested, the code is as follows:

$a=array("a,2,3","4"); 
 
if(in_array('a',$a)){ 
 
  echo "条件不成立"; 
 
}

This is not established, in the first Under the 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 that starts with the number, it will return a true value regardless of whether the first parameter is a complete element of the second parameter, that is, in_array The 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, we 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 "====="; 
}

The above is the detailed content of Usage of in_array function in php. For more information, please follow other related articles on the PHP Chinese website!

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