Content · Programming · PHP

PHP str_ends_with() Function: Usage and Examples

Learn str_ends_with() with clear PHP examples, output, practical scenarios, important behaviors, and common beginner mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP developers sometimes need to check whether a string ends with a specified suffix. This comes up when preparing data, displaying results, or integrating information with other services.

The str_ends_with() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.

What Is str_ends_with() in PHP?

Check whether a string ends with a specified suffix.

Its syntax is:

str_ends_with(string $haystack, string $needle): bool

The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.

Important: It is case-sensitive, byte-oriented, and an empty suffix always matches. Extensions alone do not prove safe file content.

Parameters:

  • $haystack — A named input or configuration argument; use the declared type and optional default shown above.
  • $needle — A named input or configuration argument; use the declared type and optional default shown above.

Example 1: Basic Usage

This first example uses a small input to demonstrate the main result of str_ends_with().

<?php

var_dump(str_ends_with("report.csv", ".csv"));

Output:

bool(true)

The output demonstrates the primary operation: check whether a string ends with a specified suffix.

Example 2: Understanding Another Case

This second example changes the input or option to reveal a different behavior of str_ends_with().

<?php

var_dump(str_ends_with("report.CSV", ".csv"));

Output:

bool(false)

Compared with the first example, this one illustrates the same operation with a different input shape or option. It is case-sensitive, byte-oriented, and an empty suffix always matches. Extensions alone do not prove safe file content.

Practical Example: A Real Application Scenario

Here is a practical use of str_ends_with() with realistic sample information. Adapt the values to match your application.

<?php

$filename = "backup.zip"; if (str_ends_with($filename, ".zip")) { echo "ZIP extension"; }

Output:

ZIP extension

This is a useful starting point, but production code should check errors and validate values before performing the operation. It is case-sensitive, byte-oriented, and an empty suffix always matches. Extensions alone do not prove safe file content.

Important Behavior and Edge Cases

It is case-sensitive, byte-oriented, and an empty suffix always matches. Extensions alone do not prove safe file content.

  • Input: Use the expected PHP types and validate user-provided values.
  • Result: Check the return value separately from any modifications to the input.
  • Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.

Comparison With Related Functions

Function or ApproachWhen to Use It
str_ends_with()Check whether a string ends with a specified suffix.
Related functionsstr_starts_with() checks prefixes; substr() extracts string sections.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming str_ends_with() behaves like every similar functionIt is case-sensitive, byte-oriented, and an empty suffix always matches. Extensions alone do not prove safe file content.
Choosing a related operation without checking differencesstr_starts_with() checks prefixes; substr() extracts string sections.
Ignoring prerequisites or return valuesVerify extensions and data sources, and handle unexpected results explicitly.

Key Takeaways

  • str_ends_with() lets you check whether a string ends with a specified suffix.
  • It is case-sensitive, byte-oriented, and an empty suffix always matches. Extensions alone do not prove safe file content.
  • str_starts_with() checks prefixes; substr() extracts string sections.
  • Check input, return values, and environmental requirements when adapting the examples.

str_ends_with() provides a focused PHP operation for check whether a string ends with a specified suffix. Start with the simple examples, then adapt the practical example with the correct checks for your project.

Share this resourceSend this page to someone who may find it useful.