Home  >  Article  >  Backend Development  >  What is the usage of in_array() in php

What is the usage of in_array() in php

王林
王林Original
2021-09-22 17:46:273232browse

The usage of in_array() in php is: [in_array(search, array)], where search specifies the value to be searched in the array, and array specifies the array to be searched. The in_array() function is used to search whether the specified value exists in the array.

What is the usage of in_array() in php

The operating environment of this article: windows10 system, php7, thinkpad t480 computer.

We all know that there are a large number of built-in functions in PHP, which are very convenient to use in many situations. There is a function in_array(), and many friends may not know its specific usage. Let's take a look at what this function does.

The in_array() function is used to search whether the specified value exists in the array, and returns true if the given value exists in the array. At the same time, the function can also set a third parameter. If the third parameter is set to true, the function will only return true if the element exists in the array and the data type is the same as the given value. If the parameter is not found in the array, the function returns false.

Let’s take a look at the specific syntax and parameters:

in_array(search,array,type)
  • 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.

Finally let’s take a look at how to use this function. The following is the specific code:

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

Recommended learning: php training

The above is the detailed content of What is the usage of in_array() in php. 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