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

Usage of in_array function in php

怪我咯
Release: 2023-03-14 07:50:02
Original
1917 people have browsed it

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 "条件成立"; 
}
Copy after login

. 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 "条件不成立"; 
 
}
Copy after login

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 "====="; 
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template