in_array() function in PHP

PHPProgrammingServer Side Programming

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!
raja
Published on 15-Oct-2018 09:28:56
Advertisements