PHP 8 reference · Array Functions

PHP in_array() Function

Check whether a value exists in an array. Pass true as the third argument for a strict (===) comparison.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

in_array(mixed $needle, array $haystack, bool $strict = false): bool

Parameters

ParameterDescription
needle The value to find.
haystack The array to search.
strict optional true also checks the type (===). Default: false

Return value

true if found.

Examples

Is it in the array?Try it yourself
<?php
$roles = ["admin", "editor"];
var_dump(in_array("editor", $roles));
var_dump(in_array("guest", $roles));

Output

bool(true)
bool(false)
Use strict modeTry it yourself
<?php
var_dump(in_array("1e1", ["10"]));       // loose: both are 10
var_dump(in_array("1e1", ["10"], true)); // strict

Output

bool(true)
bool(false)

Common mistakes

  • Loose comparison can match unexpected values; pass true as the third argument unless you need conversion.
  • For many lookups in a big list, flip it once ($set = array_flip($list)) and use isset($set[$value]).

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?