PHP str_ends_with() Function

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:

  • String: This is the main string and the function takes to checks if it ends with a substring.
  • Substring: This is a substring that uses to check if the main string ends with this.

Detail:

  • PHP Version: 8 or Higher
  • Return Value: Boolean (true or false)

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.


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: PHP, PHP-Functions,