Exploration of in_array function usage in php

Source: Internet
Author: User
This article mainly introduces the in_array function usage in php, and deeply explores the in_array function parameter matching usage, which helps you to fully understand the in_array function usage, for more information about how to use the in_array function in php, see this article. Share it with you for your reference. The details are as follows:

Today, I suddenly think that the in_array function in php has a strange usage. let's take a look at this usage. if you need a friend, let's take a simple reference. the code is as follows:

The code is as follows:

$ 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, and then tested the following situation, the code is as follows:

The code is as follows:

$ 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 we write parameter 1 as the '1' condition, it is also not true. The conclusion is as follows:

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, 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, it is also found that if the first element of your array is 0, there will be problems, see the following situation, the code is as follows:

The code is as follows:

$ A = array (0, "m ");
If (in_array ('mc6', $ ))
{
Echo "= ";
}

If you are interested, you can test and run the instances described in this article. I believe there will be new gains!

I hope this article will help you with php programming.

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.