Home >>PHP Array Functions >PHP in_array() Function

PHP in_array() Function

PHP in_array() Function

PHP in_array() function is used to check if a given input value exists in an array or not. It returns TRUE if the given value is present in the given array and FALSE if not present.

Syntax:

in_array($search, $array, $type);

Parameter Values

Parameter Description
search This is a required parameter. This parameter defines what to search for.
array This is a required parameter. This parameter defines the array to search.
type type This is an optional parameter. If this parameter is set to TRUE then it searches for the search-string and specific type in the array.

Here is an example of in_array() function in PHP:

<html>
<body>
<?php
$x = array("Devil", "Jerry", "Mickey", "Joker");
if (in_array("Jerry", $x))
  {
  echo "Match found…";
  }
else
  {
  echo "Match not found…";
  }
?>
</body>
</html>
Output:
Match found…

Example 2:

<html>
<body>
<?php
$x = array("Devil", "Jerry", "Mickey", "Joker", "Cobra", "Jade", 1000);
if (in_array("Joker",$x, TRUE))
  {
  echo "Your Match found....<br>";
  }
else
  {
  echo "Match not found !!!<br>";
  }
if (in_array("1000", $x, TRUE))
  {
  echo "Your Match found....<br>";
  }
else
  {
  echo "Match not found !!!<br>";
  }
if (in_array("1000",$x))
  {
  echo "Your Match found....<br>";
  }
else
  {
  echo "Match not found !!!<br>";
  }
?>
</body>
</html>
Output:
Your Match found....  	

No Sidebar ads