Use of in_array() function in PHP

WBOY
Release: 2023-08-26 19:46:01
forward
742 people have browsed it

in_array() 函数在 PHP 中的使用

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

Syntax

in_array(find, arr, type)
Copy after login

Parameters

  • find - The value to search for

  • arr - The array to search

  • type - The pattern to perform the search on. If this parameter is set to TRUE, it will look for the search string and specific type in the array.

Return

in_array()The function returns TRUE if the value is found in the array, otherwise it returns FALSE.

Example

The following is an example-

Real-time demonstration

<?php
$stu = array("Tom", "Amit", "Peter", "Rahul");
if (in_array("Amit", $stu)) {
   echo "Match!";
} else {
   echo "No Match!";
}
?>
Copy after login

Output

The following is the output-

Match!
Copy after login
Copy after login

Example

Let’s see another example-

Live Demonstration

<?php
$pro = array("Tom", "Amit", "Peter", "Rahul", 5, 10);
if (in_array(5, $pro, TRUE)) {
   echo "Match!";
} else {
   echo "No Match!";
}
?>
Copy after login

Output

Here is the output-

Match!
Copy after login
Copy after login

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

source:tutorialspoint.com
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