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 −

 Live Demo

<?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 −

 Live Demo

<?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!

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 24-Jun-2020

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements