PHP in_array Function | How to use in_array in PHP

Summary: In this article, you learn PHP in_array function and how to use it with example. Let’s understand this article in details with various example.

PHP in_array Definition and Usage

The PHP in_array() function is an inbuilt function. The in_array() function is used to searches an array for a specific value or used to check whether a given value exists in an array or not. furthermore, it will return TRUE if the given value is found in the given array, and otherwise, it will return FALSE.

.
Important Note: suppose that if the search parameter is a string & the type parameter is set to TRUE, then the search is case-sensitive.
The searches haystack for needle using the loose comparison unless strict is set.

Syntax:

in_array($val, $array_name ,$mode)

Parameter Values

The in_array() function in PHP accepts three parameters, out of which two parameters are compulsory and another one parameter is optional. All three parameters are described below:

Parameters Description
$val: It is a required parameter that specifies the element or value to be searched in the given array. This parameter can be of mixed type. mean that, it can be of string type or integer type, or another type. If this parameter is of string type then the search will be performed or executed in a case-sensitive manner.
$array_name: This is also a required parameter and it specifies the array in which we wish to search.
$mode: It is an optional parameter and is of boolean type. furthermore, It specifies the mode in which we would like to perform the search. If it’s set to TRUE, then the PHP in_array() function searches for the value with the same or equivalent type of value as specified by $val parameter. The default value is FALSE.
Return Value:

The in_array() function in PHP returns a boolean value i.e, TRUE if the value $val is found in the array otherwise it returns FALSE.

Technical Details

PHP Version: 4+
Return Value: It will return a TRUE value if the value is found in the array, or FALSE otherwise
PHP Changelog: PHP 4.2: The search parameter may currently be an array

PHP in_array Example

Program:1

In the given below program we search for the value "Cauliflower" in an array and output some text:

Match is found
Program:2

This program performs the search using in_array() function in non-strict mode. furthermore, that is, the last parameter $mode is set to false which is the default value. The value to be searched is of string type whereas this value in the array is of integer type not exist in the in_array() function then it returns false as the search is in non-strict mode.

match is not found
Program:3

The given program performs the search using in_array() function in PHP in strict mode. That is, the last parameter $mode is set to true and the function will now also check the type of values.

It’s found
It’s found
It’s not found
Program:4

The given program performs PHP in_array() function with strict example.

21.45 It’s found with strict check
Program:5

The given program performs PHP in_array() with an array as needle.

‘ab’ was found
‘f’ was found