Why Check the Beginning and End of a String in PHP?
\n\nImagine: you're writing a website in PHP, and you need to check if a filename ends with .jpg? Or if an email starts with admin? In the past, you had to write workarounds with substr() or regular expressions. But starting with PHP 8.0, we have an elegant solution — the str_starts_with() and str_ends_with() functions.
This isn't just syntactic sugar. These are fast, readable, and safe ways to work with strings that every PHP developer should know. In this article, we'll break them down with real examples, compare them to old approaches, and show you how to avoid common mistakes.
\n\nWhat are str_starts_with and str_ends_with?
\n\nBoth functions appeared in PHP 8.0 (released in November 2020). They solve one simple task: check if a string contains a given prefix (start) or suffix (end).
\n\nFunction Signatures
\n\nstr_starts_with(string $haystack, string $needle): bool\nstr_ends_with(string $haystack, string $needle): bool\n\n- \n
$haystack— the string to search in. \n $needle— the substring to search for. \n - Returns
trueorfalse. \n
Simple Example
\n\necho str_starts_with('Hello, world!', 'Hello'); // true\necho str_ends_with('Hello, world!', 'world!'); // true\necho str_starts_with('Hello, world!', 'world'); // false\n\nImportant: the functions are case-sensitive. str_starts_with('PHP', 'php') will return false. If you need a case-insensitive comparison, use strtolower() or strtoupper() on both strings.
Real-World Usage Examples
\n\n1. Checking File Extensions
\n\n$filename = 'report_2024.pdf';\n\nif (str_ends_with($filename, '.pdf')) {\n echo "This is a PDF document";\n} elseif (str_ends_with($filename, '.jpg')) {\n echo "This is an image";\n} else {\n echo "Unknown format";\n}\n\nIn the past, this was done with substr($filename, -4) — it was easy to make a mistake with the suffix length. Now the code reads like plain English: "string ends with".
2. Validating URLs or Emails
\n\n$email = 'user@gmail.com';\n$url = 'https://pythonlib.ru';\n\nif (str_ends_with($email, '@gmail.com')) {\n echo "Gmail user";\n}\n\nif (str_starts_with($url, 'https://')) {\n echo "Secure connection";\n}\n\n3. Filtering an Array of Strings
\n\n$files = ['image.jpg', 'document.pdf', 'photo.png', 'notes.txt'];\n\n$images = array_filter($files, function($file) {\n return str_ends_with($file, '.jpg') || str_ends_with($file, '.png');\n});\n\nprint_r($images); // ['image.jpg', 'photo.png']\n\nHow It Worked Before (and Why the New Functions Are Better)
\n\nBefore PHP 8.0, developers used several approaches. Let's compare them with the new functions.
\n\nMethod 1: substr()
\n\n// Old way: checking the start\nif (substr($string, 0, strlen('prefix')) === 'prefix') { ... }\n\n// New way\nif (str_starts_with($string, 'prefix')) { ... }\n\nDisadvantages of the old way:
\n- \n
- Verbose: you need to calculate the substring length. \n
- Risk of error with negative length (if
$needleis longer than$haystack). \n - Less readable code. \n
Method 2: strpos()
\n\n// Old way: checking the start\nif (strpos($string, 'prefix') === 0) { ... }\n\n// New way\nif (str_starts_with($string, 'prefix')) { ... }\n\nWith strpos, it's easy to make a logical error: if you don't use === 0, but just == false or == 0
Blogs
Book Recommendations
Read also
Learn Python for Automation: Best Courses & Resources 2025
GraphQL vs REST in PHP: What to Choose for Your Project in 2024?
Dates in TypeScript: How to Properly Declare a Type and Not Lose Your Mind
Rust on GitHub: Best Open-Source Projects for Developers
Removing an element from a PHP array by value: under the hood