Computer >> Computer tutorials >  >> Programming >> PHP

in_array() function in PHP


The in_array() function checks if a specified value exists in an array.

Syntax

in_array(find, arr, type)

Parameters

  • find − The value to be searched

  • arr − The array to search

  • type − The mode in which the search is to be performed. If the parameter is set to TRUE, then it looks for for the search string and specific type in the array.

Return

The in_array() function returns TRUE if the value is found in the array, or FALSE otherwise.

Example

The following is an example −

<?php
$stu = array("Tom", "Amit", "Peter", "Rahul");
if (in_array("Amit", $stu)) {
   echo "Match!";
} else {
   echo "No Match!";
}
?>

Output

The following is the output −

Match!

Example

Let us see another example −

<?php
$pro = array("Tom", "Amit", "Peter", "Rahul", 5, 10);
if (in_array(5, $pro, TRUE)) {
   echo "Match!";
} else {
   echo "No Match!";
}
?>

Output

The following is the output −

Match!