Mastering PHP’s in_array(): How to Check Values in Arrays Efficiently

This guide explains PHP's in_array() function, detailing its parameters, strict mode behavior, return values, and provides a clear example that demonstrates how to search for a value within an array and handle case‑sensitive matches.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Mastering PHP’s in_array(): How to Check Values in Arrays Efficiently

Function Overview

The in_array() function checks whether a given value ( $needle) exists in an array ( $haystack). If the optional $strict flag is omitted, the function performs a loose comparison; when $strict is TRUE, both value and type must match.

Parameters

needle : The value to search for. If it is a string, the comparison is case‑sensitive.

haystack : The array to be searched.

strict : Optional boolean. When TRUE, in_array() also checks that the type of needle matches the type of the corresponding element in haystack.

Return Value

Returns TRUE if needle is found in haystack; otherwise returns FALSE.

Example

<?php
$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
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendArrayfunctioncodingin_array
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.