Home  >  Article  >  Backend Development  >  Introduction to PHP functions: in_array() function

Introduction to PHP functions: in_array() function

WBOY
WBOYOriginal
2023-11-04 17:06:381896browse

Introduction to PHP functions: in_array() function

PHP function introduction: in_array() function

PHP is a commonly used server-side scripting language that provides a wealth of built-in functions to simplify the development process. Among them, the in_array() function is a very useful function for determining whether a specified value exists in the array. This article will introduce the usage of in_array() function in detail and provide specific code examples.

1. Basic usage of in_array() function
The in_array() function is used to search for a specified value in an array and returns a Boolean value indicating whether the value is found. Its basic syntax is as follows:

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

Parameter description:

  • $ needle: The value to search for, can be of any type.
  • $haystack: Array to search.
  • $strict (optional): The default is FALSE, indicating that data types are not distinguished. If set to TRUE, data types are strictly compared.

Return value: TRUE if the searched value is found, FALSE otherwise.

The following is a simple example showing how to use the in_array() function:

<?php
    $fruits = array("apple", "banana", "orange", "grape");

    if (in_array("apple", $fruits)) {
        echo "找到了苹果!";
    } else {
        echo "未找到苹果!";
    }

    if (in_array("watermelon", $fruits)) {
        echo "找到了西瓜!";
    } else {
        echo "未找到西瓜!";
    }
?>

Output:

找到了苹果!
未找到西瓜!

In the above example, we first define an array containing the name of the fruit Array of $fruits. Then, we use the in_array() function to determine whether the specified value exists in the array. In the first judgment condition, the value we search for is "apple". Since this value exists in the array, "Apple found!" is output. In the second judgment condition, the value we are searching for is "watermelon". Since this value does not exist in the array, "Watermelon not found!" is output.

2. Strict comparison of in_array() function
In the previous example, we used the default parameters, that is, comparison without distinguishing data types. Next, we will demonstrate how to use the in_array() function to perform strict comparisons.

<?php
    $numbers = array("1", 2, 3, "4");

    if (in_array("2", $numbers, true)) {
        echo "找到了2!";
    } else {
        echo "未找到2!";
    }
?>

Output:

未找到2!

In the above example, we defined an array $numbers containing numbers. Then, we use the in_array() function to perform a strict comparison to determine whether there is an element with a value of "2" in the array. Since there is an element with value 2 in the array, and the searched value is "2", and a strict comparison is made, FALSE is returned and "2 not found!" is output.

To sum up, the in_array() function is a very useful PHP function that can quickly determine whether a value exists in an array. Through the above introduction and sample code, I believe that readers have mastered the basic usage and precautions of the in_array() function. In actual development, rational use of this function can improve the execution efficiency and readability of the code and reduce the occurrence of errors.

The above is the detailed content of Introduction to PHP functions: in_array() function. 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