Some ideas caused by the php in_array Function

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.