PHP in_array() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, in_array() checks whether a value exists inside an array and returns true or false.

It looks at the values of the array, not the keys. It is the easiest way to answer questions like "is this color in my list of colors?". The optional third argument decides whether the types must match too.

Syntax

Call in_array() with the values you need. Start with this line.

PHP
$found = in_array("green", $colors);
CallWhat it does
in_array("green", $colors)Returns true if "green" is one of the values in $colors.
in_array("2", $numbers, true)Strict mode. The string "2" does not match the integer 2.
in_array(30, $ages)Searches the values of an array that has string keys.

The order of the arguments is needle first, then haystack. The needle is the thing you look for and the haystack is the array you search in. in_array() never changes the array.

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

Parameters

ParameterDescription
needleRequired. The value to look for.
haystackRequired. The array to search in.
strictOptional. If true, the value and its type must both match (like ===). The default is false (like ==).

Return Value

Return ValueDescription
booltrue if the needle is found in the array, otherwise false.

PHP Examples

Example 1 — Check If a Value Is in a List

This example asks whether two different words are in the same array.

PHP
<?php
$colors = ["red", "green", "blue"];

var_dump(in_array("green", $colors));
var_dump(in_array("yellow", $colors));

How It Works

  1. $colors holds three color names.
  2. "green" is one of the values, so in_array() returns true.
  3. "yellow" is not in the array, so in_array() returns false.
  4. var_dump() prints the boolean results so you can see them clearly.

Example 2 — Loose and Strict Comparison

The third argument controls whether the type must match. This matters when numbers and strings are mixed.

PHP
<?php
$numbers = [1, 2, 3];

var_dump(in_array("2", $numbers));
var_dump(in_array("2", $numbers, true));
var_dump(in_array(2, $numbers, true));

How It Works

  1. $numbers holds the integers 1, 2 and 3.
  2. Loose mode treats the string "2" as equal to the integer 2, so the first result is true.
  3. Strict mode also compares the type, and a string is not an integer, so the second result is false.
  4. Searching for the integer 2 in strict mode matches exactly, so the last result is true.

Example 3 — in_array() Searches Values, Not Keys

When an array has string keys, in_array() still looks only at the values. Use array_key_exists() to look for keys.

PHP
<?php
$ages = ["Tom" => 30, "Ann" => 25];

var_dump(in_array(30, $ages));
var_dump(in_array("Tom", $ages));
var_dump(array_key_exists("Tom", $ages));

How It Works

  1. $ages has the keys Tom and Ann and the values 30 and 25.
  2. 30 is one of the values, so the first check is true.
  3. "Tom" is only a key, not a value, so in_array() returns false.
  4. array_key_exists() looks at keys, so it finds "Tom" and returns true.

Frequently Asked Questions

It checks whether a given value exists in an array. It returns true when a match is found and false when it is not.
Values only. To check for a key, use array_key_exists() or isset().
When strict is true, in_array() uses === so both value and type must match. When it is false, PHP uses == and may convert types, so "2" matches 2.
The needle comes first and the haystack second: in_array($needle, $haystack). Many beginners swap them by mistake.
It checks the elements one by one, so it gets slower as the array grows. For very large lists, store the values as keys and use isset(), which is much faster.

Next: key()

key() returns the key of the element that the internal array pointer is pointing at.

key() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful