Home >Backend Development >PHP Tutorial >PHP8.1's new str_contains function: quickly determine whether a substring exists
PHP8.1’s new str_contains function: quickly determine whether a substring exists
In the latest PHP8.1 version, a very convenient function str_contains has been added, its function is to Quickly determine whether a string contains another substring. Compared with the previous strpos function, the str_contains function is more concise and easy to use, which can greatly improve development efficiency. This article will introduce you to the use of str_contains function and provide some code examples.
In previous versions, to determine whether a string contains another substring, the strpos function was often used. For example, if we want to determine whether the string $str contains the substring "world", we can achieve this through the following code:
if(strpos($str, 'world') !== false){ echo "包含子字符串"; }else{ echo "不包含子字符串"; }
The above code first uses the strpos function to find the substring in the string. position, if the return value is not false, it means that the substring exists in the string, and we can output "contains substring". Otherwise, output "does not contain substring".
However, there are some problems with using the strpos function for judgment. First, the return value of the strpos function may be 0, indicating that the substring appears at the beginning of the string, which can be confused with the return value when the substring does not exist. Secondly, the strpos function requires writing more code to implement judgment, which does not seem intuitive enough. Therefore, in the new version of PHP8.1, the str_contains function was introduced to solve these problems.
The use of the str_contains function is very simple. You only need to pass in two parameters. The first parameter is the string to be searched, and the second parameter is the substring to be searched. The function returns a bool value indicating whether the substring exists in the string.
The following is a sample code using the str_contains function:
$str = "Hello, world!"; if(str_contains($str, 'world')){ echo "包含子字符串"; }else{ echo "不包含子字符串"; }
In the above code, if the string $str contains the substring "world", then "contains substring" is output; otherwise , output "does not contain substring". It can be seen that compared to using the strpos function, using the str_contains function is more intuitive and concise.
In addition to being used to determine whether a single string contains a substring, the str_contains function can also be used for each element in an array or string. The following is a sample code:
$fruits = ['apple', 'banana', 'orange']; if(str_contains($fruits, 'banana')){ echo "数组中包含子字符串"; }else{ echo "数组中不包含子字符串"; }
In the above code, determine whether the array $fruits contains the substring "banana". If it does, output "the array contains the substring"; otherwise, output "array" does not contain substring".
In summary, the new str_contains function in PHP8.1 provides us with a simple and intuitive way to determine whether a string contains substrings. Its use is very simple, you only need to pass in two parameters and determine the return value. Whether you are testing a single string or each element in an array or string, the str_contains function provides a convenient solution. When you start using PHP8.1 version, remember to try this convenient function!
The above is the detailed content of PHP8.1's new str_contains function: quickly determine whether a substring exists. For more information, please follow other related articles on the PHP Chinese website!