Home  >  Article  >  Backend Development  >  Detailed explanation of php in_array() checking whether a certain value exists in the array

Detailed explanation of php in_array() checking whether a certain value exists in the array

墨辰丷
墨辰丷Original
2018-05-30 15:54:199209browse

PHP sometimes needs to determine whether a certain value exists in an array. We can directly use the PHP built-in function in_array() to realize the determination. The php in_array function is used to check whether a certain value exists in the array. If it exists, it returns true, otherwise it returns false. This article introduces the basic syntax and usage examples of the in_array function. Interested friends can refer to it.

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

in_array Check whether a certain value exists in the array

Basic syntax :

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

Search needle

in haystack

Parameter introduction


Parameter 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:

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

The second condition of running online fails because in_array() is case-sensitive, so the above program is displayed as :

Got Irix

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

phpImplement uploading excel tables and obtain data

PHP’s method of implementing regular regular verification helper public class

PHP’s method of implementing web content html tag completion and filtering

The above is the detailed content of Detailed explanation of php in_array() checking whether a certain value exists in the array. For more information, please follow other related articles on the PHP Chinese website!

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