Exploring the In_array function usage in PHP _php skills

Source: Internet
Author: User

This paper probes into the usage of In_array function in PHP. Share to everyone for your reference. Specifically as follows:

Today, I suddenly think of PHP In_array function has a strange usage, let's take a look at this usage, the need for a simple reference to friends, the code is as follows:

Copy Code code as follows:
$a =array ("1,2,3", "4");
if (In_array (1, $a)) {

echo "Conditional establishment";
}

This condition is indeed set up, according to my thinking the logic above the condition only in the value of parameter 1 is "1,2,3" and 4, but it is strange that he actually set up, and then test the following situation, the code is as follows:
Copy Code code as follows:
$a =array ("a,2,3", "4");

if (In_array (' a ', $a)) {

echo "condition is not tenable";

}

This is not tenable, in the first test case, if the parameter 1 written ' 1 ' condition is also not tenable, the conclusion is:

In PHP, if the first argument is a number--no quotes, the second array argument contains a string that begins with that number. The true value is returned regardless of whether the first argument is a complete element of the second argument, that is, in_ Does the array function not always match the whole element in my thinking logic, or do I not know the data type well enough?

At the same time, also found that if your array of the first element of 0, there will be problems, look at the following situation, the code is as follows:

Copy Code code as follows:
$a =array (0, "M");
if (In_array (' mc6 ', $a))
{
echo "=====";
}

Interested friends can test run the example described in this article, I believe there will be a new harvest!

I hope this article will help you with your PHP program design.

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.