PHP 8 reference · String Functions
PHP str_ends_with() Function
Check whether a string ends with a given substring, such as a file extension. Case-sensitive; added in PHP 8.0.
PHP 8 Since 8.0
Syntax
str_ends_with(string $haystack, string $needle): bool
Parameters
| Parameter | Description |
|---|---|
haystack |
The string to check. |
needle |
The suffix to look for. |
Return value
true or false.
Examples
<?php
var_dump(str_ends_with("photo.JPG", ".jpg"));
var_dump(str_ends_with(strtolower("photo.JPG"), ".jpg"));
Output
bool(false) bool(true)
<?php
$dir = "uploads";
if (!str_ends_with($dir, "/")) {
$dir .= "/";
}
echo $dir, "\n";
Output
uploads/
Version history
| Version | Change |
|---|---|
| PHP 8.0 | Added |
In other languages
str.endswith()· Pythonstring.endsWith()· JavaScript
Related
str_starts_with()· Check whether a string begins with a given substring. Case-sensitive and returns true or false; added in PHP 8.0.str_contains()· Check whether a string contains a substring and return true or false. Case-sensitive; added in PHP 8.0.- PHP Strings · tutorial