Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
string
communitycreator

What is str_ends_with() in PHP?

Dario Vincenzo Tarantini

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The str_ends_with function in PHP checks if a string ends with a certain string.


This function is available in PHP 8 and higher.

Visual representation of str_ends_with() function

Syntax


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

Parameters

The str_ends_with() function requires two strings as parameters.

Return value

This function returns true if successful and false if it is not.

Code

<?php
if (str_ends_with('hello', 'lo')) {
echo "'hello' end with 'lo'\n";
}
if (!str_ends_with('hello', 'ol')) {
echo "'hello' doesn't end with 'ol'\n";
}

Output

'hello' ends with 'lo'
'hello' doesn't end with 'ol'

RELATED TAGS

php
string
communitycreator

CONTRIBUTOR

Dario Vincenzo Tarantini

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring