"Does my string start with this word?" — the eternal question of a PHP developer
\n\nImagine a situation: you are writing a URL parser, processing incoming requests, or simply filtering a list of files. And then the task arises — you need to check if a string starts with a certain set of characters. It would seem, what could be simpler? But it was not to be. Until recently, PHP did not have a built-in function for this simplest operation. Developers improvised in all sorts of ways: some cut the string using substr(), some searched for the position using strpos(), and some even delved into the wilds of regular expressions.
In this article, we will break down all the ways to check the beginning of a string in PHP. You will learn not only how to do it "elegantly" in modern versions, but also how to solve the task on "legacy" projects where PHP 5 is still in use. Let's go!
\n\nThe most correct way: str_starts_with() (PHP 8.0+)
\n\nIf your project runs on PHP 8.0 or newer — you are lucky. You have access to the native function str_starts_with(), which does exactly what it says on the tin. It takes two arguments: the original string and the substring we are looking for at the beginning. Returns true or false.
<?php\n$url = "https://pythonlib.ru/php-tutorial";\n\nif (str_starts_with($url, "https://")) {\n echo "Link is secure, using HTTPS";\n} else {\n echo "Warning! Not an HTTPS connection";\n}\n// Outputs: Link is secure, using HTTPS\n?>\n\nWhy is this the best option?
\n- \n
- Readability — the code speaks for itself. You don't need to comment on what this line does. \n
- Performance — the function is optimized at the PHP C-code level. It works faster than manual checks. \n
- Safety — no issues with multibyte encodings (unless you work with UTF-8 in a special way, but more on that later). \n
By the way, the function has a "twin sister" — str_ends_with(), which checks the end of a string. But that's a topic for another article.
Classic of the genre: strpos()
\n\nBefore the advent of str_starts_with(), the most popular method was using the strpos() function. It searches for the first occurrence of a substring in a string and returns its position (a number). If the substring is not found, it returns false.
The trick is that if the substring is at the very beginning, its position will be 0. And 0 in PHP is a falsy value. Therefore, the check must be strict:
\n\n<?php\n$string = "Hello, world!";\n\nif (strpos($string, "Hello") === 0) {\n echo "The string starts with Hello";\n} else {\n echo "No, the beginning is different";\n}\n?>\n\nImportant! Always use the strict comparison operator ===, not ==. Why? Imagine you are searching for a substring that doesn't exist. strpos() will return false. If you write strpos($string, "test") == 0, then false == 0 will give true (because PHP casts types). And your code will say the string starts with "test", even though it doesn't. A classic trap for beginners!
Pros and cons of strpos()
\n- \n
- ✅ Works on all PHP versions, starting from 4. \n
- ✅ Does not require installing extensions. \n
- ❌ The code is less readable — you need to remember about strict comparison. \n
- ❌ If used incorrectly (without ===), it introduces hard-to-find bugs. \n
Grandfather's method: substr()
\n\nAnother method that came to us from the depths of history. The idea is simple: cut a piece from the string of the same length as the searched substring, and compare them.
\n\n<?php\n$filename = "report_2024.pdf";\n\n$prefix = "report_";\nif (substr($filename, 0, strlen($prefix)) === $prefix) {\n echo "This is the report for 2024";