Hello PHP Devs,

I would like to propose the new basic function: str_contains.

The goal of this proposal is to standardize on a function, to check weather
or not a string is contained in another string, which has a very common
use-case in almost every PHP project.
PHP Frameworks like Laravel create helper functions for this behavior
because it is so ubiquitous.

There are currently a couple of approaches to create such a behavior, most
commonly:
    <?php
        strpos($haystack, $needle) !== false;
        strstr($haystack, $needle) !== false;
        preg_match('/' . $needle . '/', $haystack) != 0;

All of these functions serve the same purpose but are either not intuitive,
easy to get wrong (especially with the !== comparison) or hard to remember
for new PHP developers.

The proposed signature for this function follows the conventions of other
signatures of string functions and should look like this:

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

This function is very easy to implement, has no side effects or backward
compatibility issues.
I've implemented this feature and created a pull request on GitHub ( Link:
https://github.com/php/php-src/pull/5179 ).

To get this function into the PHP core, I will open up an RFC for this.
But first, I would like to get your opinions and consensus on this proposal.

What are your opinions on this proposal?

Kind regards,
Philipp Tanlak

Reply via email to