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

Some thoughts caused by php in_array function_PHP tutorial

WBOY
Release: 2016-07-20 11:03:12
Original
989 people have browsed it

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
 代码如下 复制代码

$a=array(0,"m");
if(in_array('mc6',$a))
{
 echo "=====";
}

$a=array(0,"m");
if(in_array('mc6',$a))
{
echo "=====";
}

http://www.bkjia.com/PHPjc/445324.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445324.htmlTechArticleToday I suddenly thought of a strange usage of the in_array function in php. Let’s take a look at this usage. Friends in need can simply refer to it. The code is as follows Copy the code $a=array...
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