PHP str_ends_with

Summary: in this tutorial, you’ll learn how to use the PHP str_ends_with() function to check if a string ends with a substring.

Introduction to the PHP str_ends_with() function

The str_ends_with() function performs a case-sensitive search and checks if a string ends with a substring.

Here’s the syntax of the str_ends_with() function:

str_ends_with ( string $haystack , string $needle ) : bool
Code language: PHP (php)

The str_ends_with() function has two parameters:

  • $haystack is the input string to check.
  • $needle is the substring to search for in the input string.

The str_ends_with() function returns true if the $haystack ends with the $needle or false otherwise.

Note that every string ends with an empty string. Therefore, if the $needle is an empty (''), the str_ends_with() always returns true.

The str_ends_with() function has only been available since PHP 8.0.0. If you use PHP with a lower version, you can polyfill the function like this:

<?php if (!function_exists('str_ends_with')) { function str_ends_with($haystack, $needle) { return $needle !== '' ? substr($haystack, -strlen($needle)) === $needle : true; } }
Code language: PHP (php)

PHP str_ends_with() function examples

Let’s take some examples of using the PHP str_ends_with() function.

1) Using PHP str_ends_with() function with an empty substring example

The following example returns true because every string ends with an empty string:

<?php echo str_ends_with('Hello there', '');
Code language: PHP (php)

Output:

1
Code language: PHP (php)

2) Using PHP str_ends_with() function with single character example

The following example uses the PHP str_ends_with() function to check if the string 'PHP tutorial' ends with the letter 'l':

<?php $str = 'PHP tutorial'; $substr = 'l'; $result = str_ends_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result ending with $substr";
Code language: PHP (php)

Output:

The PHP tutorial is ending with l
Code language: PHP (php)

3) Using the PHP str_ends_with() function with multiple characters example

The following example uses the PHP str_ends_with() function to check if the string 'PHP tutorial' ends with the string 'tutorial':

<?php $str = 'PHP tutorial'; $substr = 'tutorial'; $result = str_ends_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result ending with $substr";
Code language: PHP (php)

Output:

The PHP tutorial is ending with tutorial
Code language: PHP (php)

4) Case-sensitive example

It’s important to notice that the str_ends_with() function carries a case-sensitive search. For example:

<?php $str = 'PHP tutorial'; $substr = 'Tutorial'; $result = str_ends_with($str, $substr) ? 'is' : 'is not'; echo "The $str $result starting with $substr";
Code language: PHP (php)

Output:

The PHP tutorial is not ending with Tutorial
Code language: PHP (php)

Summary

  • Use the PHP str_ends_with() function to perform a case-sensitive search and check if a string ends with a substring.
Did you find this tutorial useful?