Home > Backend Development > PHP Tutorial > php in_array function usage (example)

php in_array function usage (example)

王林
Release: 2023-04-09 12:40:01
Original
3435 people have browsed it

php in_array function usage (example)

Function introduction:

in_array() function is used to search whether the specified value exists in the array. Returns TRUE if the value is found in the array, FALSE otherwise.

(Recommended tutorial: php graphic tutorial)

Function syntax:

bool in_array(mixed $needle, array $haystack[, bool $strict = FALSE])
Copy after login

Parameter introduction:

  • needle Required. Specifies the value to search for in the array.​

  • haystack Required. Specifies the array to search.

  • #strict Optional. If this parameter is set to TRUE, the in_array() function checks whether the data being searched is of the same type as the value of the array.

Code example one:

Search for the value "phpcn" in the array and output some text:

<?php
$sites = array("Google", "phpcn", "Taobao", "Facebook"); 
if (in_array("phpcn", $sites)){
    echo "找到匹配项!";
}else{
    echo "没有找到匹配项!";
}
?>
Copy after login

Output result:

找到匹配项!
Copy after login

(Learning video recommendation: php video tutorial)

Code example two:

<?php
$people = array("Bill", "Steve", "Mark", "David");

if (in_array("23", $people, TRUE))
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }
if (in_array("Mark",$people, TRUE))
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }

if (in_array(23,$people, TRUE))
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }
?>
Copy after login

Output result:

匹配未找到
匹配已找到
匹配未找到
Copy after login

The above is the detailed content of php in_array function usage (example). For more information, please follow other related articles on the PHP Chinese website!

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