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.
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 IrixSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
