Content · Programming · PHP

PHP in_array() Function: Usage and Examples

Understand in_array() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to check whether a specific value is present in an array. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The in_array() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is in_array() in PHP?

Check whether a specific value is present in an array.

Its syntax is:

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

Parameters used by this operation:

  • $needle — The value supplied for this argument affects the operation.
  • $haystack — The collection being examined or updated.
  • $strict — When true, comparisons check both value and type.

Use strict=true to prevent unintended matches between numbers and numeric strings. For keys, use array_key_exists() instead.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$roles = ["reader", "editor", "admin"];
var_dump(in_array("editor", $roles, true));

Output:

bool(true)

The output shows the result of the first call. Check the printed keys and values as well as their order.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$ids = [1,2,3];
var_dump(in_array("2", $ids));
var_dump(in_array("2", $ids, true));

Output:

bool(true)
bool(false)

Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.

Practical Example: Check an entered role against a small trusted allowlist

Check an entered role against a small trusted allowlist. Save the code in a PHP file and adapt the sample data to your project.

<?php

$allowedStatuses = ["pending", "approved", "rejected"];
$requestedStatus = "approved";
if (in_array($requestedStatus, $allowedStatuses, true)) { echo "Status is supported"; }

Output:

Status is supported

The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.

Important Behavior and Edge Cases

Use strict=true to prevent unintended matches between numbers and numeric strings. For keys, use array_key_exists() instead.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
in_array()Check whether a specific value is present in an array
Related functionsarray_search() returns the first matching key; isset() tests whether a key maps to a non-null value.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming input and result have identical keysCheck whether the function reindexes keys or retains original associations.
Confusing mutation with a returned copyUse strict=true to prevent unintended matches between numbers and numeric strings. For keys, use array_key_exists() instead.
Choosing a related function with different behaviorarray_search() returns the first matching key; isset() tests whether a key maps to a non-null value.

Key Takeaways

  • in_array() helps you check whether a specific value is present in an array.
  • Use strict=true to prevent unintended matches between numbers and numeric strings. For keys, use array_key_exists() instead.
  • array_search() returns the first matching key; isset() tests whether a key maps to a non-null value.
  • Use the practical example as a starting point, but validate real application inputs.

in_array() is useful when you need to check whether a specific value is present in an array. Use its documented rules and the examples above to choose the right behavior for your data.

Share this resourceSend this page to someone who may find it useful.