Last updated on February 17, 2023
PHP 8 built-in function str_ends_with() accepts two parameters and checks if the second parameter’s position is at the end of the first parameter. It returns a Boolean value (true or false). Let’s say the first is a sentence and the second is a word.
Syntax:
str_ends_with(string $sentence, string $word);
Parameters:
Detail:
Example:
The str_ends_with()
function checks if the provided word exists at the end of the sentence. To understand it, look at the example below.
$response = str_ends_with("PHP is programming language", "language");
var_dump($response);
// Output: bool(true)
In this example, we are checking if the word “language” position is at the end of the sentence. In the response we got true because the str_ends_with()
function found the word “language” at the end of the sentence.
Let’s change the value of the second argument and check the response.
$response = str_ends_with("PHP is programming language", "programming");
var_dump($response);
// Output: bool(false)
In this example, we replaced the second argument value with “programming”. Since the sentence doesn’t end with “programming” that’s why we got false in the output.