PHP str_starts_with and str_ends_with: A Guide for Beginners

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

Why Check the Beginning and End of a String in PHP?

\n\n

Imagine: 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.

\n\n

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\n

What are str_starts_with and str_ends_with?

\n\n

Both 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\n

Function Signatures

\n\n
str_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 true or false.
  • \n
\n\n

Simple Example

\n\n
echo str_starts_with('Hello, world!', 'Hello'); // true\necho str_ends_with('Hello, world!', 'world!');   // true\necho str_starts_with('Hello, world!', 'world');  // false
\n\n

Important: 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.

\n\n

Real-World Usage Examples

\n\n

1. 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\n

In 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".

\n\n

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\n

3. 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\n

How It Worked Before (and Why the New Functions Are Better)

\n\n

Before PHP 8.0, developers used several approaches. Let's compare them with the new functions.

\n\n

Method 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\n

Disadvantages of the old way:

\n
    \n
  • Verbose: you need to calculate the substring length.
  • \n
  • Risk of error with negative length (if $needle is longer than $haystack).
  • \n
  • Less readable code.
  • \n
\n\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\n

With strpos, it's easy to make a logical error: if you don't use === 0, but just == false or == 0

Blogs

Book Recommendations