Search value exist in array or not using php

Search value exist in array or not using php

                In php there is a inbuild function called in_array() which is used to search data is existing or not in array. It returns boolean value i.e "TRUE" if the search value is found in the array, and "FALSE" if the search value is not found.

Syntax :
 bool in_array ( $value, $array_name ,$mode )  

Parameters: The in_array() function accepts three parameters, out of which two are compulsory and other one is optional. All three parameters are described following:
  1. $value: This is the compulsory parameter for in_array () function which contains the searching value. This can be of mixed type i.e, it can be of string type or integer type or any other type. it depends on user that which type of search value wants to find in array.
  2. $array_name: This is a compulsory parameter and it specifies the array in which we want to search.
  3. $mode: This is third one which is an optional parameter and is of boolean type. This parameter specifies the mode in which we want to perform the search. If it is set to TRUE, then the in_array() function searches for the value with the same type of value as specified by $val parameter. The default value of this parameter is FALSE.
Return Value: The in_array() function returns a boolean value i.e, TRUE if the value $val is found in the array otherwise it returns FALSE.

Below programs to illustrate The in_array() function in PHP:

<?php
$marks = array(100, 65, 70, 87);
 
if (in_array("100", $marks))
  {
  echo "found";
  }
else
  {
  echo "not found";
  }
?>

in the program we are searching 100 in array marks weather it exist in array or not. after successfull running when its found the 100 in array then function returns "TRUE" as a "found" string message.

Output:
found




No comments:

Post a comment

If you have any doubts regarding the post. Please let me know.