How to use str_ends_with function in PHP?

The str_ends_with function in PHP is used to check if a string ends with a certain string.

Note: This function is only available in PHP 8 onwards.

Syntax

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

Parameters

  • haystack: the string where we want to search
  • needle: the string we will search for

Return value

The function returns true if the given string ends with the given needle. Otherwise, it returns false.

Code

In this example, we will check our string ends with Educative!:

<?php
var_dump(str_ends_with("Hello, Educative!", "Educative!"));
?>

Expected output

bool(true)

Free Resources