Checking the beginning of a string in PHP: from substr to str_starts_with

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

"Tell me more about this part": how to find out what a string starts with in PHP?

\n\n

Every developer has encountered the task at least once: "Does this string start with a certain word, character, or prefix?" It seems like a small thing, but the logic of routing, URL validation, email validation, or parsing user input can depend on it. In PHP, as in any other language, there are several ways to solve this problem. But which one is correct? And which one is the most performant? And what do you do if you have an old version of PHP?

\n\n

In this article, we will break down all popular ways to check the beginning of a string in PHP, starting from classics like substr and strpos and ending with the modern str_starts_with, which appeared in PHP 8.0. You will learn not only "how" but also "why" you should choose one method or another. Let's go! 🚀

\n\n

Why check the beginning of a string at all?

\n\n

Imagine you are writing a log parser, processing URLs, or filtering files by extension. Or maybe you need to filter out all email addresses starting with "admin@". Or check if an API request came with the correct prefix. In all these cases, you need one thing: to find out if the string contains a given substring at the very beginning.

\n\n

It seems like a trivial task, but not knowing the nuances can lead to bugs that are hard to catch. Especially if you accidentally use strpos without checking for false. Let's break down each method in order.

\n\n

Method #1: str_starts_with (PHP 8.0+) — modern and obvious

\n\n

Starting from PHP 8.0, the language has a special function str_starts_with(). It does exactly what it says on the tin: checks if a string starts with a given substring. And it does it quickly, readably, and without pitfalls.

\n\n
<?php\n$url = "https://pythonlib.ru";\nif (str_starts_with($url, "https://")) {\n    echo "The string starts with https://";\n} else {\n    echo "The string does NOT start with https://";\n}\n// Outputs: The string starts with https://\n?>
\n\n

Why is this great?

\n
    \n
  • ✅ The code reads like plain English: "string starts with".
  • \n
  • ✅ Returns only true or false — no extra checks needed.
  • \n
  • ✅ Works correctly with multibyte encodings (UTF-8).
  • \n
  • ✅ Case-sensitive. If you need a case-insensitive check, see str_starts_with(strtolower(...)).
  • \n
\n\n

This method is the absolute leader if you have PHP 8.0 or higher. But what if your project is still running on PHP 7? Read on.

\n\n

Method #2: substr — an old friend is better than two new ones

\n\n

Before the advent of str_starts_with, many used substr to compare the beginning of a string. The idea is simple: cut a piece of the required length from the string and compare it with the desired substring.

\n\n
<?php\n$string = "Hello, world!";\n$prefix = "Hello";\n\nif (substr($string, 0, strlen($prefix)) === $prefix) {\n    echo "The string starts with 'Hello'";\n} else {\n    echo "The string does NOT start with 'Hello'";\n}\n?>
\n\n

Pros:

\n
    \n
  • Works on any version of PHP.
  • \n
  • Clear logic.
  • \n
\n

Cons:

\n
    \n
  • You need to remember strlen() — it's easy to make a mistake with the length.
  • \n
  • If the string is shorter than the prefix, substr will return an empty string, and the comparison will fail (but won't break).
  • \n
  • Less readable than the modern alternative.
  • \n
\n\n

Nevertheless, this is a completely working option that won't let you down. Just don't forget to use strict comparison ===.

\n\n

Method #3: strpos — tricky but dangerous

\n\n

Another classic approach is to use strpos(). The function searches for the first occurrence of a substring

Blogs

Book Recommendations