PHP str_ends_with() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP 8.0 and later, str_ends_with() checks whether a string ends with a given text.

It returns true or false and is case-sensitive.

Syntax

Call str_ends_with() with the values you need. Start with this line.

PHP
$isPdf = str_ends_with($file, ".pdf");
CallWhat it does
str_ends_with("report.pdf", ".pdf")Returns true.
str_ends_with("report.pdf", ".doc")Returns false.

An empty needle always returns true. For the start of a string, use str_starts_with().

Library declarations
PHP
str_ends_with(string $haystack, string $needle): bool

Parameters

ParameterDescription
haystackRequired. The string to check.
needleRequired. The ending text you expect.

Return Value

Return ValueDescription
booltrue if the haystack ends with the needle, otherwise false.

PHP Examples

Example 1 — Check a File Extension

Test the end of a file name.

PHP
<?php
$file = "report.pdf";

if (str_ends_with($file, ".pdf")) {
    echo "This is a PDF", PHP_EOL;
}

How It Works

  1. Pick a file name. It ends with .pdf.
  2. Call str_ends_with(). String first, ending second.
  3. Get true. The ending matches.
  4. Run the if block. The message prints.

Example 2 — Case and Empty Needle

Case matters, and an empty ending always matches.

PHP
<?php
echo str_ends_with("photo.PNG", ".png") ? "yes" : "no", PHP_EOL;
echo str_ends_with("photo.PNG", "") ? "yes" : "no", PHP_EOL;

How It Works

  1. Compare .PNG with .png. The letters differ in case.
  2. Get false. The first line prints no.
  3. Use an empty needle. Nothing is required at the end.
  4. Get true. The second line prints yes.

Example 3 — Filter Files in a Loop

Keep only the files that end with .png.

PHP
<?php
$files = ["logo.png", "notes.txt", "photo.png"];

foreach ($files as $file) {
    if (str_ends_with($file, ".png")) {
        echo $file, PHP_EOL;
    }
}

How It Works

  1. Loop through the list. Each name goes into $file.
  2. Test the ending. Only .png names pass.
  3. Skip other files. notes.txt is ignored.
  4. Print matches. Two names are shown.

Frequently Asked Questions

str_ends_with() checks if a string ends with a given substring and returns true or false.
str_ends_with(string $haystack, string $needle): bool. The full string comes first, then the ending to test.
It was added in PHP 8.0. For older versions, use substr() or substr_compare() to check the ending.
Yes. Lowercase both strings first with strtolower() if you want to ignore case.
It returns true, because every string ends with an empty string.

Next: str_getcsv()

str_getcsv() parses one line of CSV text into an array.

str_getcsv() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful