in_array()

in_array() — Checks if a value exists in an array.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Searches for needle in haystack using loose comparison unless strict is set.

Signature

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

Parameters

ParameterTypeWhat it takes
$needlemixedThe searched value.
$haystackarrayThe array.
$strictboolIf the third parameter strict is set to true then the in_array() function will also check the types of the needle in the haystack.

Return value

Returns true if needle is found in the array, false otherwise.

Example

A runnable snippet:

$os = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $os)) { echo "Got Irix"; } if (in_array("mac", $os)) { echo "Got mac"; }

Output:

Got Irix

See also

array_search(), array_key_exists()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page in_array() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.