W3docs

in_array()

Learn how PHP's in_array() checks whether a value exists in an array, how loose vs. strict comparison works, and the type-juggling gotchas to avoid.

What in_array() does

in_array() checks whether a given value exists anywhere in an array and returns a boolean: true if the value is found, false if it isn't. It searches the array's values (not its keys), which makes it the go-to function for questions like "is this option allowed?" or "has this user already been added to the list?".

This page covers the signature, how loose vs. strict comparison changes the result, the type-juggling gotchas that trip people up, and when to reach for a different function instead.

Syntax

in_array(mixed $needle, array $haystack, bool $strict = false): bool
ParameterDescription
$needleThe value to search for.
$haystackThe array to search in.
$strictWhen true, the types must also match (uses === instead of ==). Defaults to false.

The function returns true if $needle is found in $haystack, otherwise false.

Basic example

<?php

$fruits = ["apple", "banana", "orange"];

if (in_array("banana", $fruits)) {
    echo "Found!";
} else {
    echo "Not found.";
}
// Output: Found!

in_array() is case-sensitive for strings — "Apple" would not match "apple":

php— editable, runs on the server

Loose vs. strict comparison

By default in_array() uses loose comparison (==), so values of different types can match. Pass true as the third argument to require an exact type match (===):

<?php

$numbers = [1, 2, 3, 4];

// Loose: the string "1" equals the integer 1
var_dump(in_array("1", $numbers));        // bool(true)

// Strict: "1" (string) is not identical to 1 (int)
var_dump(in_array("1", $numbers, true));  // bool(false)

Use strict mode whenever your array mixes types, or when matching the wrong type would be a bug — for example checking a user-supplied ID against a list of integer IDs.

The type-juggling gotcha

Loose comparison can produce surprising results. A classic example searches for an empty string or 0 in a list of strings:

<?php

$values = [0, "", "foo", "bar"];

var_dump(in_array("0", $values));       // bool(true)  — "0" == 0
var_dump(in_array("0", $values, true)); // bool(false) — different types

Here "0" (string) loosely equals the integer 0 already in the array, so the loose search returns true. Strict mode removes the ambiguity.

Note: PHP 8 changed how strings and numbers compare. In PHP 7, in_array(0, ["foo", "bar"]) returned true because "foo" was cast to 0. In PHP 8+ the same call returns false. When in doubt, pass $strict = true.

Getting the position instead of true/false

in_array() only tells you whether a value exists. If you also need its key, use array_search() — it returns the key on success and false on failure:

<?php

$fruits = ["apple", "banana", "orange"];

$key = array_search("banana", $fruits);
var_dump($key); // int(1)

To check whether a particular key (not value) exists, use array_key_exists() or isset() instead.

When to use a different function

  • Need the matching value's position? → array_search()
  • Checking for a key rather than a value? → array_key_exists() or isset()
  • Filtering an array down to matching elements? → array_filter()
  • Searching a very large array repeatedly? Flip it to keys with array_keys() and use isset(), which is faster than scanning with in_array().

Summary

  • in_array($needle, $haystack) returns true/false for whether a value exists in an array.
  • It searches values, is case-sensitive for strings, and uses loose comparison by default.
  • Pass true as the third argument for strict (type-aware) matching — preferred when types matter.
  • Reach for array_search() when you need the key, and array_key_exists()/isset() when checking keys.

Practice

Practice
What is the purpose of the 'in_array' function in PHP?
What is the purpose of the 'in_array' function in PHP?
Was this page helpful?