str_starts_with and str_ends_with functions in PHP

str_starts_with and str_ends_with functions in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

str_starts_with and str_ends_with functions in PHP

In modern PHP development, the utility of string manipulation functions has evolved significantly since the introduction of str_starts_with and str_ends_with in version 8.0. Before this release, developers typically relied on regular expressions or substring comparisons to achieve similar results, often resulting in less readable code and potential performance overhead due to regex engine invocation. The native boolean return values provided by these functions offer a cleaner, more predictable approach for conditional logic involving string boundaries.

To understand the mechanics behind str_starts_with, consider the function signature: bool str_starts_with(string $haystack, string $needle). This function returns true if the $haystack begins exactly with the $needle, and false otherwise. The logic is straightforward but requires attention to edge cases regarding case sensitivity and character matching. For instance, in your example where $str = '|apples}' and you check for the start delimiter '|', the function performs a direct byte-by-byte comparison of the initial sequence. It does not perform any pattern matching; if you need to match wildcards or ignore case, these functions alone are insufficient without preprocessing the input or using regex.

Similarly, str_ends_with(string $haystack, string $needle) validates the suffix. When checking if the string ends with '}', the function ensures that the last characters of the $haystack match the $needle exactly. A common pitfall arises when dealing with empty needles. In PHP 8.1 and later, both functions treat an empty string as a valid prefix or suffix, returning true for any given haystack. This behavior aligns with mathematical definitions where every sequence starts and ends with an empty set, but it can introduce logic errors if your application assumes a non-empty delimiter is always present.

Here is how you might implement these checks in a practical scenario without resorting to regex:

$str = '|apples}';

// Check start
if (str_starts_with($str, '|')) {
    echo 'String starts with pipe.';
}

// Check end
if (str_ends_with($str, '}')) {
    echo 'String ends with closing brace.';
}

One significant advantage of using these native functions over custom implementations is performance. While modern regex engines are highly optimized, invoking them simply to check the first or last few characters incurs unnecessary overhead. Native string comparison functions operate in C and are generally faster for simple prefix/suffix checks. However, if you are processing millions of strings where the needle length varies significantly, profiling your specific workload remains essential. Sometimes a simple strncmp call might be preferable depending on the exact use case and PHP version constraints.

Another consideration is internationalization. These functions operate on bytes by default. If your application relies heavily on UTF-8 characters and needs to match based on grapheme clusters rather than individual bytes, you must ensure that the $needle and $haystack are consistent in their encoding representation before calling the function. Mismatched normalization forms can lead to unexpected false results even when visual inspection suggests a match.

For projects utilizing the Laravel framework, these functions integrate seamlessly into conditional blocks within controllers or service classes. While Laravel does not strictly require using these specific functions for routing or validation, leveraging them in custom logic improves code clarity compared to older patterns like strpos($str, $needle) === 0. If you are maintaining legacy code on PHP 7.x, you would need to implement these checks manually using substr_compare, but upgrading to PHP 8.0+ is generally recommended for accessing this built-in functionality.

For more complex string validation needs or if you require a centralized library of such utilities, resources like https://laravelcompany.com provide extensive documentation on extending Laravel's core capabilities with custom helpers that build upon these native functions.

Our network