New function str_starts_with() in PHP8: method to determine the beginning of a string

王林
Release: 2023-05-17 09:08:01
Original
883 people have browsed it

With the release of PHP8, we ushered in some new functions and features. One of the very useful new functions is str_starts_with(). It is used to determine whether a string begins with another specified string. In this article, we will discuss this new function and how to use it in PHP.

In the past, if we wanted to determine whether a string started with another string, we needed to use the substr() function to get a part of the original string, and then combine this substring with the specified prefix character Strings are compared. This process requires several steps and comparison operations, and the code is relatively lengthy. However, now with the str_starts_with() function, we can reduce this process to a single line of code.

The use of str_starts_with() function is very simple. It only takes two parameters, the first parameter is the string to check, and the second parameter is the prefix string to look for. The function returns true if the string to be checked begins with the specified prefix string, false otherwise.

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

bool str_starts_with(string $string, string $prefix)

The following is an example that demonstrates how to use str_starts_with() Function:

$string = "Hello world!";
$prefix1 = "Hello";
$prefix2 = "Foo";

if (str_starts_with($string, $prefix1)) {

echo "The string starts with prefix 1.";
Copy after login
Copy after login

}

if (str_starts_with($string, $prefix2)) {

echo "The string starts with prefix 2.";
Copy after login
Copy after login

} else {

echo "The string does not start with prefix 2.";
Copy after login
Copy after login

}

In the above example, we first define a primitive string $string, and then define two prefix strings $prefix1 and $prefix2. We use the str_starts_with() function twice to check if the string starts with these prefix strings. The first check returns true because $string starts with "Hello"; the second check returns false because $string does not start with "Foo".

It should be noted that the str_starts_with() function is case-sensitive. This means that if the case of the specified prefix string does not match the actual string, the function will return false. If we want to ignore case for initial comparison, we can use the strtolower() function to convert all strings to lowercase before comparing.

The following is an example that demonstrates how to use the strtolower() function to perform a case-insensitive beginning comparison:

$string = "Hello world!";
$prefix1 = "Hello";
$prefix2 = "hello";

if (str_starts_with(strtolower($string), strtolower($prefix1))) {

echo "The string starts with prefix 1.";
Copy after login
Copy after login

}

if (str_starts_with(strtolower($string), strtolower($prefix2))) {

echo "The string starts with prefix 2.";
Copy after login
Copy after login

} else {

echo "The string does not start with prefix 2.";
Copy after login
Copy after login

}

In the above example, We first use the strtolower() function to convert all strings to lowercase, and then use the str_starts_with() function to perform a start comparison. The first comparison returns true because "Hello" and "hello" are equal in lowercase; the second comparison also returns true because $string starts with "hello".

In short, the str_starts_with() function is a very useful new function because it provides a simple and fast way to determine whether a string starts with the specified prefix string. If you are using PHP8 or higher, we recommend you use this new function to simplify your code and improve performance.

The above is the detailed content of New function str_starts_with() in PHP8: method to determine the beginning of a string. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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