PHP uses the In_array function to check if a value exists in the array _php tutorial

Source: Internet
Author: User
Tags array definition

PHP uses the In_array function to check if a value exists in the array


This article mainly introduces PHP using the In_array function to check if there is a value in the array, a more detailed analysis of the function of the In_array function, definition and related use of skills and considerations, with a certain reference value, the need for friends can refer to the next

This example describes how PHP uses the In_array function to check if a value exists in an array. Share to everyone for your reference. The specific analysis is as follows:

PHP uses the In_array () function to check if a value exists in the array, return TRUE if it exists, or return false, which is very useful, and I'll dive into the In_array () function for you.

When writing a piece of code in PHP recently, it is used to determine whether a value is in another set of values. The In_array function is used to check if a value exists in the array. Directly through the conceptual understanding of the fuzzy, you can use specific examples to understand its role.

The syntax is as follows:

?

1

BOOL In_array (mixed needle, array array [, BOOL strict])

Parameter description:

Parameters Description
Needle The value that needs to be searched in the array, if it is a string, is case-sensitive
Array The array to retrieve
Strict Optional, if set to TRUE, the value types in the needle and array are also checked

Example 1:

?

1

2

3

4

5

6

7

8

9

$os = Array ("Mac", "NT", "Irix", "Linux");

if (In_array ("Irix", $os)) {

echo "Got Irix";

}

if (In_array ("Mac", $os)) {

echo "Got mac";

}

?>

The result of the above code execution is:

Got Irix

The second condition fails because In_array () is case-sensitive.

Example 2:

?

1

2

3

4

5

6

$europe = Array ("United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Denmark");

if (In_array ("Us", $europe)) {

echo "True";

}

?>

As with the above, the execution result is true.

Example 3: Strict type checking examples

?

1

2

3

4

5

6

7

8

9

$a = Array (' 1.10 ', 12.4, 1.13);

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

echo "' 12.4 ' found with strict check";

}

if (In_array (1.13, $a, true)) {

echo "1.13 found with strict check";

}

?>

The output is:

1.13 Found with strict check

Example 4: Applying an array in an array

?

1

2

3

4

5

6

7

8

9

10

11

12

$a = array (' P ', ' h '), Array (' P ', ' r '), ' o ');

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

echo "' ph ' was found";

}

if (In_array (Array (' f ', ' I '), $a)) {

echo "' fi ' was found";

}

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

echo "' O ' was found";

}

?>

The output is:

' ph ' was found
' O ' was found

Its specific usage is as follows:

?

1

BOOL In_array (mixed $needle, array $haystack [, bool $strict = FALSE])

Search for needle in haystack, if not set strict then use a loose comparison.

Note: Since php5.4. The array definition is replaced by array () array[].

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/973922.html www.bkjia.com true http://www.bkjia.com/PHPjc/973922.html techarticle PHP uses the In_array function to check if a value exists in the array this article mainly introduces PHP using the In_array function to check if there is a value in the array, more detailed analysis of the In_array letter ...

  • Related Article

    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.