wppaste
WordPress

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

Since
5.9.0
Source
wp-includes/compat.php:543

Checks whether a haystack string ends with a given needle, matching case exactly. WordPress only defines this function when the site's PHP version doesn't already provide it natively (PHP 8.0+), so it behaves as a drop-in for the built-in str_ends_with(). Pair it with str_starts_with() or str_contains() when you need to test the other end or the middle of a string.

Polyfill for str_ends_with() function added in PHP 8.0.

Description

Performs a case-sensitive check indicating if the haystack ends with needle.

Compatibility

WordPress
since 5.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$haystackstring
The string to search in.
$needlestring
The substring to search for in the $haystack.

Return value

bool
True if $haystack ends with $needle, otherwise false.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Check if a post permalink ends with its slug

Confirm that the permalink for the first sample post ends with the expected slug before building custom rewrite logic around it.

$permalink = get_permalink( 1 );

if ( str_ends_with( $permalink, 'hello-world' ) ) {
	echo esc_html( 'Permalink ends with hello-world: ' . $permalink );
} else {
	echo esc_html( 'Permalink does not end with hello-world: ' . $permalink );
}

Reject uploaded file names with a disallowed extension

Loop over a small batch of candidate file names and keep only the ones ending in an approved image extension.

$filenames = array( 'holiday-photo.jpg', 'notes.txt', 'banner.PNG', 'archive.zip' );
$allowed   = array( '.jpg', '.jpeg', '.png', '.gif' );

foreach ( $filenames as $filename ) {
	$is_allowed = false;

	foreach ( $allowed as $extension ) {
		if ( str_ends_with( strtolower( $filename ), $extension ) ) {
			$is_allowed = true;
			break;
		}
	}

	printf( '%s: %s<br>', esc_html( $filename ), $is_allowed ? 'allowed' : 'rejected' );
}

Lowercase the file name first since str_ends_with() itself is case-sensitive.

Common problems and fixes · 3

Why does str_ends_with() return true when I pass in two empty strings?

The source special-cases an empty haystack: if $haystack is '' it returns the result of '' === $needle, so an empty needle matches. This is correct PHP 8.0 behavior, not a bug, but it surprises code that expects an empty needle to always fail. - Add an explicit check for $needle !== '' before calling str_ends_with() if an empty needle should never count as a match.

Why doesn't str_ends_with() match when the casing is different?

The function does a plain === comparison between substr( $haystack, -$len, $len ) and $needle, with no case folding anywhere in the source. 'Photo.JPG' will not match a needle of '.jpg'. - Lowercase both sides first: str_ends_with( strtolower( $haystack ), strtolower( $needle ) ).

Why do I get a fatal error about str_ends_with() already being declared?

This is a polyfill that WordPress only loads when the running PHP version doesn't already provide str_ends_with() natively. If a plugin or library also defines a plain function named str_ends_with() without checking function_exists(), it collides with either the native PHP 8.0+ version or WordPress's own polyfill. - Wrap any custom definition in a function_exists( 'str_ends_with' ) check. - Rename the custom function instead of shadowing a core/native name.

Alternatives and related functions

str_starts_with
When you need to check the beginning of a string instead of the end.
str_contains
When the substring can appear anywhere in the string, not just at the very end.
substr_compare
When you want the native PHP function directly and don't need WordPress's version-detection polyfill logic.

Performance profile

How much work a call to str_ends_with() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–10

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 12.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
49

49 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost str_ends_with() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6–10none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev126–101
8.5126–101
8.4126–1013 fewer instructions than PHP 8.3
8.3156–131
8.2156–131
8.1156–131
7.4156–131

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Used by · 49

Show all 49

Source code

	function str_ends_with( $haystack, $needle ) {		if ( '' === $haystack ) {			return '' === $needle;		} 		$len = strlen( $needle ); 		return substr( $haystack, -$len, $len ) === $needle;	}

Changelog

Introduced in 5.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/compat.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.