Home  >  Article  >  Backend Development  >  The role of str_contains function in PHP8.0

The role of str_contains function in PHP8.0

WBOY
WBOYOriginal
2023-05-14 08:51:05938browse

Over time, programming languages ​​are constantly upgraded in terms of functionality and performance. PHP is a common server-side language. Starting with PHP 7, the language has received many differences, including enhanced type inference, null coalescing operator, better exception handling and high speed.

PHP 8.0 is the latest version of PHP, which brings some new functions and language features, the most notable of which is the str_contains function. Let’s take a closer look at the role of str_contains function in PHP 8.0.

str_contains function - Introduction

The new str_contains function is one of the new functions in PHP 8.0. Its function is to determine whether a string contains another substring. This function alone can check whether a substring within a given string exists, so it can replace the strpos() function.

str_contains is case-aware, which means it is case-sensitive, so only an exact match will return true. If you want case insensitivity, you can use a function like mb_strpos() or preg_match().

Syntax Description

The following is the syntax of the str_contains() function:

bool str_contains( string $haystack, string $needle);

Comment: The The function returns a Boolean value, returning true if $needle is found in $haystack.

Parameter description:

  • $haystack: the string to be searched
  • $needle: the substring to be found

Example

The following example demonstrates how to use the str_contains() function to search for substrings in a string:

$str1 = "Hello world!";

/ / Check if "world" is in $str1
if (str_contains($str1, "world")) {

echo "存在!";

} else {

echo "不存在!";

}

Output:

exists!

Advantages

The str_contains() function is a powerful tool, not least because it improves the readability of the code. If you use this function, you will:

  1. Simplify your code: Using the str_contains() function is easier than using the strpos() function because it is more straightforward and you do not need to calculate offsets or use Conditional statements.
  2. Improve code readability: When you use the str_contains() function in your code, you can more quickly determine the purpose of your code, and what string you are searching for.
  3. Determine the required operation: If you need to search a string and perform a specific operation, the str_contains() function is very useful as it allows you to add additional operations to it, such as replacing spaces or removing certain content.

Notes

When using the str_contains() function, please remember the following points:

  1. Strings are case-sensitive: If If you want to search for a case-insensitive string, use a function similar to mb_strpos().
  2. Status symbols#@, etc. may not be accurate when searching. Assuming that a string may contain the searched status symbol, it is recommended to replace it with other characters.
  3. It only applies to PHP 8.0 and above.

Conclusion

The str_contains() function in PHP 8.0 is a quick and easy way to perform string operations in PHP. Use it to make your code more readable and easier to understand. At the same time, you need to understand the limitations when using the str_contains() function.

The above is the detailed content of The role of str_contains function in PHP8.0. 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