The str_ends_with() function in PHP 8.4 and PHP 8.5 works exactly the same as it did when introduced in PHP 8.0. Its purpose is to perform a case-sensitive check to see if a string ends with a specific substring.
Basic Usage and Examples
The function signature is str_ends_with(string $haystack, string $needle): bool. It returns true if the $haystack ends with $needle, and false otherwise.
Here are the key examples from the official documentation:
Example 1: Basic Check
<?php
$string = The lazy fox jumped over the fence ;if (str_ends_with($string, fence )) {
echo "The string ends with fence ";
}
// Output: The string ends with fence
?>
Example 2: Case-Sensitivity
<?php
$string = The lazy fox jumped over the fence ;if (str_ends_with($string, Fence )) {
echo The string ends with "Fence" ;
} else {
echo "Fence" was not found because the case does not match ;
}
// Output: "Fence" was not found because the case does not match
?>
Example 3: Empty String
<?php
if (str_ends_with( abc , )) {
echo "All strings end with the empty string";
}
// Output: All strings end with the empty string?>
Key Points to Remember
- Case-Sensitive: "fence" will match, but "Fence" will not.
- Binary-Safe: The function can safely handle binary data without corrupting it.
- No Version-Specific Changes: There are no behavioral changes or deprecations for this function in PHP 8.4 or 8.5. It is a stable, native part of the language since PHP 8.0.