How to use in_array in PHP 8


The in_array function checks whether a value exists in an array.

in_array in PHP 8

in_array(mixed $needle, array $haystack, bool $strict = false): bool


The in_array function takes as first parameter the value that you are looking for, the $needle. The $haystack paramter is the array which should be searched for the $needle. The last parameter $strict indicates whether or not the types of the compared values should match too.

  1. $needle:

    This paramater holds the value that you are looking for. If it is a string, the search is case sensitive.
  2. $haystack:

    This paramter is the array that you want to look for in.
  3. $strict (optional):

    If this value is set to true, the comparison of the values will also require matching types to yield true.

The in_array returns true if the $needle could be found in the $haystack. Otherwise it will return false.


$haystack = array('one', 'two', 'three');

$needle = 'two';

$result = in_array($needle, $haystack);

echo strval($result);


In this example, the in_array function returns true because the $needle with the value two can be found in the $haystack.