Home >Backend Development >PHP Tutorial >php in_array() Check whether a certain value exists in the array Detailed explanation_php example

php in_array() Check whether a certain value exists in the array Detailed explanation_php example

WBOY
WBOYOriginal
2016-12-05 13:28:251108browse

php in_array() checks whether a certain value exists in the array

in_array checks whether a certain value exists in the array

Basic grammar:

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

Search needle in haystack

Parameter introduction

Parameters Description
needle Required. Specifies the value to search for in the array. If it is a string, the comparison is case-sensitive.
haystack Required. Specifies the array to search.
strict Optional. If this parameter is set to true, the in_array() function also checks whether the type of needle is the same as that in haystack.

Return value

Returns TRUE if needle is found, otherwise returns FALSE.

Example:

<&#63;php
$os = array(
  "Mac",
  "NT",
  "Irix",
  "Linux"
);
if (in_array("Irix", $os)) {
  echo "Got Irix";
}
if (in_array("mac", $os)) {
  echo "Got mac";
}
&#63;>

Running the second condition online fails because in_array() is case-sensitive, so the above program displays:

Got Irix

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn