Home >Backend Development >PHP Problem >How to use in_array in php? (Usage introduction)

How to use in_array in php? (Usage introduction)

青灯夜游
青灯夜游Original
2020-09-28 15:25:156259browse

In php, the in_array() function is used to search whether a specified value exists in the array, the syntax is "in_array(search,array,type)"; if the search parameter is a string, and the type parameter is set to TRUE, the search is case-sensitive.

How to use in_array in php? (Usage introduction)

Recommended: "PHP Video Tutorial"

in_array() function searches whether the specified value exists in the array.

Syntax

in_array(search,array,type)

Parameters:

  • ##search Required. Specifies the value to search for in the array.

  • array Required. Specifies the array to search.

  • #type Optional. If this parameter is set to true, it is checked whether the type of the searched data and the value of the array are the same.​

Description

  • Returns true if the given value search exists in the array array. If the third parameter is set to true, the function returns true only if the element exists in the array and has the same data type as the given value.

  • If the parameter is not found in the array, the function returns false.

  • If the search parameter is a string and the type parameter is set to true, the search is case-sensitive.

Return value: Returns TRUE if the value is found in the array, otherwise returns FALSE.

Example:


<?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>";
  }
?>

Output:

匹配未找到
匹配已找到
匹配未找到

Related recommendations:

php training

The above is the detailed content of How to use in_array in php? (Usage introduction). 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