PHP str_ends_with()
Published Jul 7, 2023
Contribute to Docs
The str_ends_with() function checks if a string ends with a given substring.
Syntax
str_ends_with( $string , $substring )
The str_ends_with() function has two required parameters:
$string: Specifies the string to check.$substring: Specifies the substring to check for in the$string.
The str_ends_with() function returns a boolean value which is true if $string ends with $substring, and false otherwise.
Example
The following example uses the str_ends_with() function to check if the "fiddlesticks" ends with "sticks" and then prints to the console:
<?phpif (str_ends_with('fiddlesticks', 'sticks')) {echo " fiddlesticks end with sticks ";}?>
The example will result in the following output:
fiddlesticks end with sticks
Codebyte Example
This example is runnable and uses the str_ends_with() function:
Note 1: The second echo statement will be true as all strings end with empty strings.
Note 2: The third echo statement will be false because the case does not match.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours