Example
Check if a string ends with a specific substring:
html
<?php
$txt = "I really love PHP!";
var_dump(str_ends_with($txt, "PHP!")); //
bool(true)
?>
Definition and Usage
The str_ends_with() function checks if a string ends with a specific substring.
**Note:**This function is case-sensitive.
**Note:**This function is binary-safe.
Syntax
text
str_ends_with(string1, string2)
Parameter Values
| Parameter | Description |
|---|---|
| string1 | Required. Specifies the main string to be searched |
| string2 | Required. Specifies the substring to search for |
Technical Details
| Return Value: | Returns a boolean |
|---|---|
| PHP Version: | 8+ |
More Examples
Example
Showing case-sensitivity:
html
<?php
$txt = "I really love PHP!";
var_dump(str_ends_with($txt, "php!")); //
bool(false)
?>
Example
All strings ends with an empty string and will always return true:
html
<?php
$txt = "PHP";
var_dump(str_ends_with($txt, "")); //
bool(true)
?>