What is str_ends_with() in PHP?

The str_ends_with function in PHP checks if a string ends with a certain string.


This function is available in PHP 8 and higher.

Visual representation of str_ends_with() function

Syntax


str_ends_with(string $haystack, string $needle): bool

Parameters

The str_ends_with() function requires two strings as parameters.

Return value

This function returns true if successful and false if it is not.

Code

<?php
if (str_ends_with('hello', 'lo')) {
echo "'hello' end with 'lo'\n";
}
if (!str_ends_with('hello', 'ol')) {
echo "'hello' doesn't end with 'ol'\n";
}

Output

'hello' ends with 'lo'
'hello' doesn't end with 'ol'

Free Resources