Backend Development 5 min read

Understanding and Using PHP's in_array() Function

This article explains PHP's in_array() function, detailing its syntax, parameters, and return values, and provides multiple code examples demonstrating basic usage, strict type comparison, and practical tips for efficiently checking the presence of values in arrays.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding and Using PHP's in_array() Function

Basic usage of in_array()

in_array() function is used to search for a specified value in an array and returns a boolean indicating whether the value was found. Its basic syntax is as follows:

<code>bool in_array (mixed $needle, array $haystack [, bool $strict = FALSE])</code>

Parameter description:

$needle : The value to search for; can be of any type.

$haystack : The array to search.

$strict (optional): Defaults to FALSE, meaning type is not considered. If set to TRUE, a strict type comparison is performed.

Return value: Returns TRUE if the value is found, otherwise FALSE.

How to use in_array() function:

<code><?php
    $fruits = array("apple", "banana", "orange", "grape");

    if (in_array("apple", $fruits)) {
        echo "Found apple!";
    } else {
        echo "Apple not found!";
    }

    if (in_array("watermelon", $fruits)) {
        echo "Found watermelon!";
    } else {
        echo "Watermelon not found!";
    }
?>
</code>

Output:

<code>Found apple!
Watermelon not found!
</code>

In this example, we first define an array $fruits containing fruit names, then use in_array() to check whether specific values exist in the array.

Strict comparison with in_array()

The previous examples used the default non‑strict comparison. The following demonstrates strict comparison by setting the third parameter to TRUE.

<code><?php
    $numbers = array("1", 2, 3, "4");

    if (in_array("2", $numbers, true)) {
        echo "Found 2!";
    } else {
        echo "2 not found!";
    }
?>
</code>

Output:

<code>2 not found!
</code>

Here, the array $numbers contains both strings and integers. Because strict comparison is enabled, the string "2" does not match the integer 2, so the function returns FALSE.

The in_array() function is a useful PHP utility for quickly determining whether a value exists in an array, improving code efficiency and readability.

PHP learning recommendations

Vue3+Laravel8+Uniapp beginner to advanced tutorial

Vue3+TP6+API social e‑commerce system development tutorial

Swoole from beginner to mastery recommended courses

Workerman+TP6 real‑time chat system limited‑time offer

backendPHParrayTutorialin_array
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.