Take the State of PHP 2026 Survey →

str_contains View as Markdown

Implemented (PHP 8.0)
Authors
Philipp Tanlak, philipp.tanlak@gmail.com
Date
2020-02-17
Version
1
Source
wiki.php.net/rfc/str_contains

Vote

Add str_contains as described closed
Yes
43
No
9
52 voters
ajf No 2020-03-02 19:10
alcaeus Yes 2020-03-02 09:42
alec No 2020-03-02 11:32
as Yes 2020-03-03 16:37
asgrim Yes 2020-03-02 13:59
ashnazg Yes 2020-03-05 18:50
beberlei Yes 2020-03-02 11:50
bishop Yes 2020-03-02 15:49
bwoebi No 2020-03-03 05:12
carusogabriel Yes 2020-03-08 22:22
derick Yes 2020-03-06 11:03
diegopires Yes 2020-03-06 16:57
duncan3dc Yes 2020-03-02 21:30
ekin Yes 2020-03-02 10:45
fibbarth Yes 2020-03-02 15:35
galvao Yes 2020-03-02 09:50
gasolwu Yes 2020-03-03 07:39
geekcom Yes 2020-03-02 18:06
girgias Yes 2020-03-02 10:02
googleguy No 2020-03-07 18:34
heiglandreas No 2020-03-03 08:18
jhdxr No 2020-03-09 13:45
kalle Yes 2020-03-02 12:31
kguest Yes 2020-03-03 22:47
klaussilveira Yes 2020-03-03 19:02
kocsismate Yes 2020-03-02 11:00
kriscraig Yes 2020-03-02 09:19
lcobucci Yes 2020-03-03 08:07
malukenho Yes 2020-03-02 20:13
marandall Yes 2020-03-02 13:58
mariano Yes 2020-03-02 10:45
mcmic Yes 2020-03-03 13:39
narf No 2020-03-10 09:32
nicolasgrekas Yes 2020-03-03 12:50
nikic Yes 2020-03-02 08:52
ocramius Yes 2020-03-02 08:48
pmjones Yes 2020-03-02 14:41
pollita No 2020-03-11 14:16
ralphschindler Yes 2020-03-03 17:21
ramsey Yes 2020-03-02 14:15
rasmus No 2020-03-13 17:42
reywob Yes 2020-03-02 18:01
ruudboon Yes 2020-03-04 06:45
salathe Yes 2020-03-03 16:19
santiagolizardo Yes 2020-03-04 15:11
sergey Yes 2020-03-02 08:48
sirsnyder Yes 2020-03-02 09:09
tandre Yes 2020-03-04 14:08
trowski Yes 2020-03-02 18:24
villfa Yes 2020-03-02 16:56
yunosh Yes 2020-03-02 11:20
zimt Yes 2020-03-05 14:42

Discussions on the mailing list

[RFC] [DISCUSSION] str_contains
by Philipp Tanlak · 5 messages
[RFC] [VOTE] str_contains
by Philipp Tanlak · 1 message
[RFC] [VOTE] str_contains is accepted
by Philipp Tanlak · 3 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

str_contains checks if a string is contained in another string and returns a boolean value (true/false) whether or not the string was found.

The typical way to check if a string is contained in another is mostly done by using the functions strpos or strstr. Because this feature is such a common use-case in almost every project, it should deserve its own dedicated function: str_contains.

Repurposing strpos and strstr for this use-case has a few down sides. Either, they are:

  • not very intuitive for a reader
  • easy to get wrong (especially with the !== comparison)
  • or hard to remember for new PHP developers.

Because of that, many PHP frameworks offer a helper function for this behavior, because it is so ubiquitous. This indicates the significance and the necessity pretty well.

Proposal

This RFC proposes the new basic function: str_contains

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

str_contains takes a $haystack and a $needle as arguments, checks if $needle is found in $haystack and returns a boolean value (true/false) whether or not the $needle was found.

The proposed function as code:

<?php
 
str_contains("abc", "a"); // true
str_contains("abc", "d"); // false
 
// $needle is an empty string
str_contains("abc", "");  // true
str_contains("", "");     // true

Regarding empty string values as $needle:

As of PHP 8, behavior of “” in string search functions is well defined, and we consider “” to occur at every position in the string, including one past the end. As such, both of these will (or at least should) return true. The empty string is contained in every string. - Nikita Popov

Case-insensitivity and multibyte strings

In a recent discussion in the internals mailing list, we came to the conclusion, there is no need for a multibyte variant of this function (e.g. mb_str_contains). The reason behind this is: A multibyte variant of this function would behave no different than the non-multibyte function. Multibyte variants behave differently when the offset/position has relevance at which the string was found. As this is not the case for this function, there is no need for that.

Concerning the case-insensitivity for this function: This might be a feature for the future, as the relevance for that is much less compared to the case-sensitive function. With that in mind, the only valid variants would be str_contains/mb_str_icontains. I assume PHP developers would be confused if this RFC offered such inconsistent variants of this function, so it's better to start small and iterate upon that.

Backward Incompatible Changes

No backwards incompatible changes inside php itself.

There might be incompatibilities, if this function was implemented in the user-land code. But this issue would be noticed by the developer quickly as such global functions are added rather early in the application boot process. This is also the reason why this feature is proposed for PHP 8, as developers test their frameworks against new major versions more specifically.

Proposed PHP Version(s)

PHP 8

Voting

Vote: Add str_contains as described — Yes: 43 · No: 9 (closed)

Implementation

The str_contains function has been implemented in this pull request: https://github.com/php/php-src/pull/5179