Facebook Page Twitter Page LinkedIn Page
× PHP Tutorial PHP Advanced PHP Resources


The PHP String str_ends_with() function is used to check if a string ends with a given substring. It performs case-sensitive check and returns true if the string ends with specified substring, else returns false.

Note: It is a binary-safe function. This function is new in PHP 8.

Syntax

str_ends_with(str1, str2)

Parameters

str1 Required. Specify the string to check
str2 Required. Specify the substring to check whether the string ends with or not.

Return Value

Returns true if the string ends with specified substring, else returns false.

Example:

In the below example, str_ends_with() function is used to check whether the given string ends with specified substring or not.

<?php
$str1 = "Hello";

//checking whether $str1 ends with empty string
if(str_ends_with($str1, "")) {
  echo "Every string ends with an empty string.\n";
}

//checking whether $str1 ends with "lo"
if(str_ends_with($str1, "lo")) {
  echo "$str1 ends with 'lo'.\n";
}

//case-sensitive check -checking 
//whether $str1 ends "LO"
if(!str_ends_with($str1, "LO")) {
  echo "str_ends_with() performs case-sensitive check.\n";
}
?>

The output of the above code will be:

Every string ends with an empty string.
Hello ends with 'lo'.
str_ends_with() performs case-sensitive check.

❮ PHP String functions