Usage and precautions of the php array function in_array-php Tutorial

Source: Internet
Author: User
Usage and precautions of the php array function in_array
This article introduces the usage example of the php array function in_array and some precautions during use. For more information, see.

There are many functions to operate arrays in php, and in_array is one of them.

In_array functionIt is used to check whether an array of values exists. that is, it can determine whether a specified value exists in the current array.

In_array (PHP 4, PHP 5)

In_array-check whether a value exists in the array

Description: bool in_array (mixed $ needle, array $ haystack [, bool $ strict])

Search for needle in haystack. If yes, TRUE is returned. otherwise, FALSE is returned.

If the value of the third strict parameter is TRUE, the in_array () function checks whether the needle type is the same as that in haystack. Tip 1: If the needle is a string, the comparison is case sensitive. Tip 2: Before PHP version 4.2.0, needle cannot be an array.

Example 1, in_array ()

     

The second condition fails because in_array () is case sensitive, so the above program is displayed as: Got Irix

Example 2: in_array () strict type check example

     

Output: 1.13 found with strict check

Example 3, in_array () uses arrays as needle

     

Output: 'Ph 'was found' o 'was found

Note the following when using the php array function in_array:

Suppose: declare an array: $ arr = array (*);

So there is: in_array (0, $ arr) = true, which is not in line with the reality. Clever, do you understand?

Solution: in_array (strval (0), $ arr, true.

This article uses a few small examples to help you learn and understand the usage of the php array function in_array, and describes the details that need attention.

Programmer's house. I wish you all a better learning experience.

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.