Home >Backend Development >PHP Tutorial >New string methods in PHP8.0: str_starts_with and str_ends_with

New string methods in PHP8.0: str_starts_with and str_ends_with

PHPz
PHPzOriginal
2023-05-14 11:40:40958browse

New string methods in PHP8.0: str_starts_with and str_ends_with

PHP8.0 is the latest version of the PHP programming language, which brings many exciting new features and improvements. Among them, str_starts_with() and str_ends_with() are two very useful string methods. This article will introduce you to their functions, usage and examples.

str_starts_with() method

str_starts_with() method is used to check whether a string starts with the specified substring, if so, it returns true, otherwise it returns false.

This is the syntax of the str_starts_with() method:

bool str_starts_with ( string $haystack , string $needle )

Among them, the $haystack parameter is the string to be checked, $needle is the substring to be checked.

Here are some examples of the str_starts_with() method:

Example 1

To check if a string starts with the substring "Hello":

$myString = "Hello World";
if (str_starts_with($myString, "Hello")) {

echo "字符串以 Hello 开头。";

} else {

echo "字符串不以 Hello 开头。";

}

// Output: "The string starts with Hello."

Example 2

To check whether the string starts with the number "123":

$myString = "123ABC";
if (str_starts_with($myString, "123")) {

echo "字符串以 123 开头。";

} else {

echo "字符串不以 123 开头。";

}

// Output: "String starts with 123." "

str_ends_with() method

str_ends_with() method is used to check whether a string ends with the specified substring. If so, it returns true, otherwise it returns false.

This is the syntax of the str_ends_with() method:

bool str_ends_with (string $haystack, string $needle)

Among them, the $haystack parameter is the string to be checked, $needle is the substring to be checked.

Here are some examples of the str_ends_with() method:

Example 1

To check if a string ends with the substring "World":

$myString = "Hello World";
if (str_ends_with($myString, "World")) {

echo "字符串以 World 结尾。";

} else {

echo "字符串不以 World 结尾。";

}

// Output: "String ends with World."

Example 2

To check whether the string ends with the substring "789":

$myString = "ABC789" ;
if (str_ends_with($myString, "789")) {

echo "字符串以 789 结尾。";

} else {

echo "字符串不以 789 结尾。";

}

// Output: "String with 789 End."

Conclusion

The str_starts_with() and str_ends_with() methods are very useful string methods in PHP8.0. They allow developers to more conveniently check the beginning and end of a string. ending, thereby improving code readability and performance.

If you are using PHP8.0 or higher, it is strongly recommended that you learn these two methods and use them in your development projects.

The above is the detailed content of New string methods in PHP8.0: str_starts_with and str_ends_with. 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