PHP 8: str_starts_with and str_ends_with — How to Check the Start and End of a String

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

No More Workarounds: Getting to Know str_starts_with and str_ends_with in PHP 8

\n\n

Have you ever written code like if (strpos($str, 'prefix') === 0) and felt it looked a bit clumsy? Or maybe you spent time deciphering substr($str, -3) === 'end' to check the end of a string? If so — I have great news for you. Starting with PHP 8.0, the language introduced two elegant and intuitive functions: str_starts_with() and str_ends_with().

\n\n

In this article, we'll break them down thoroughly. You'll learn how they work, how they differ from old approaches, and why you should use them in every modern PHP project. We'll also look at real-world examples from a developer's life — from checking URLs to email validation and working with file paths.

\n\n

What are str_starts_with() and str_ends_with()?

\n\n

These are built-in PHP 8 functions that allow you to check whether a string starts with a given substring or ends with it. They return a boolean value (true or false), making them ideal for use in conditions.

\n\n

Here is their basic syntax:

\n\n
str_starts_with(string $haystack, string $needle): bool\nstr_ends_with(string $haystack, string $needle): bool
\n\n

Where $haystack is the string you are checking, and $needle is the substring you are looking for at the beginning or end.

\n\n

Simple Example

\n\n
<?php\n$string = "Hello, world!";\n\n// Check if the string starts with "Hello"\nif (str_starts_with($string, "Hello")) {\n    echo "The string starts with 'Hello'"; // This code will execute\n}\n\n// Check if the string ends with "world!"\nif (str_ends_with($string, "world!")) {\n    echo "The string ends with 'world!'"; // And this one too\n}\n?>
\n\n

Simple, right? No extra calculations, no === 0 or strlen(). Just a check and a result.

\n\n

Why is This Important? Problems with Old Approaches

\n\n

Before PHP 8, developers had to improvise. The most popular way to check the start of a string looked like this:

\n\n
if (strpos($string, 'prefix') === 0) { ... }
\n\n

What's the problem here? strpos() searches for a substring anywhere in the string, not just at the beginning. Yes, the === 0 check filters out incorrect results, but the code itself doesn't convey the developer's intent. You look at it and think: "Hmm, is he checking if it starts with... or just searching?" — this reduces readability.

\n\n

And checking the end of a string was even less intuitive:

\n\n
if (substr($string, -3) === 'end') { ... }
\n\n

Here you need to know the exact length of the substring (-3), which leads to errors if you specify the wrong number. Plus, if the substring is longer than the string itself — substr() will simply return an empty string, and the condition will never be true.

\n\n

The new str_starts_with() and str_ends_with() functions solve all these problems:

\n
    \n
  • Readability: The code speaks for itself.
  • \n
  • Reliability: No need to think about substring length or empty strings.
  • \n
  • Performance: The functions are optimized at the PHP core level.
  • \n
\n\n

Real-World Examples in Web Development

\n\n

Let's look at where these functions can be useful in practice. I've picked a few scenarios you've likely encountered in your work.

\n\n

1. Checking URL Prefixes (Routing, Redirects)

\n\n

Suppose you're writing a simple router and want to handle URLs that start with /admin:

\n\n
<?php\n$uri = $_SERVER['REQUEST_URI'];\n\nif (str_starts_with($uri, '/admin')) {\n    echo "You are in the admin panel";\n    // Load the admin                

Blogs

Book Recommendations