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

WBOY
Release: 2023-03-03 07:16:01
Original
1086 people have browsed it

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;>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!