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:
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:
Notes
When using the str_contains() function, please remember the following points:
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!