What's the most efficient test of whether a PHP string ends with another string?

What's the most efficient test of whether a PHP string ends with another string?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

What's the most efficient test of whether a PHP string ends with another string?

Developers often optimize code by assuming that substr() is the fastest method to check if a string ends with a specific suffix. The standard idiom looks like this:

$endsWith = substr( $str, -strlen( $test ) ) == $test;

While this approach works correctly and has been the de facto standard for decades in PHP, it is not necessarily the most efficient solution, especially when dealing with long strings or high-throughput applications. The primary bottleneck here lies in how substr() operates: it allocates a new string buffer to hold the extracted portion before comparing it. For very large strings or suffixes, this memory allocation and copying step adds unnecessary overhead.

PHP introduced a dedicated function for exactly this scenario with str_ends_with() in version 8.0. This is not just a syntactic convenience; it represents a significant architectural shift that can impact performance profiles. Unlike the manual substr approach, str_ends_with() operates directly on the underlying string data without requiring intermediate allocation of a substring buffer for comparison purposes.

If you are running PHP 8.0 or later, replacing the substr pattern with str_ends_with() is almost always the right choice. Here is how the code looks in practice:

$endsWith = str_ends_with( $str, $test );

This function returns a boolean value, just like the manual comparison, but it handles the logic internally to minimize memory usage. The performance gain becomes particularly noticeable when you are iterating over thousands of records or validating large payloads in a real-time API endpoint. In these scenarios, every byte saved and every allocation avoided compounds into measurable latency improvements.

However, there is an important caveat regarding backward compatibility. If your application must support PHP versions prior to 8.0, you cannot use str_ends_with() without triggering a fatal error. In those cases, the substr method remains the only viable option, but you should be aware that it is inherently less efficient due to the allocation cost. Some projects might consider polyfilling this behavior or upgrading their PHP version entirely to unlock native support for such string operations.

Another consideration involves the length of the $test variable. When checking if a massive 50MB file path ends with ".txt", the manual substr approach creates a copy of the last 4 characters. While modern garbage collectors handle short-lived objects well, the cumulative effect in tight loops can still degrade performance. Native functions like str_ends_with() are generally implemented in C with optimized logic to avoid these intermediate steps where possible.

It is also worth noting that this optimization applies specifically to ASCII or UTF-8 string handling within PHP's native engine. If you are dealing with complex multi-byte encodings or need to handle null bytes at the end of strings, the behavior remains consistent across both methods, but the internal implementation details differ significantly.

For developers maintaining legacy codebases, switching to str_ends_with() is a low-risk refactoring if your environment supports it. It improves readability by removing the need for explicit length calculations and negative indexing in substr(). For new projects on PHP 8.0+, adopting native string utility functions should be part of your standard coding guidelines to ensure maximum efficiency from day one.

Our network