# Array arguments for str functions

- Thread: https://externals.io/message/130319
- Original archive: https://news-web.php.net/php.internals/130319
- Started by Barel on 2026-03-15T07:20:38+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #130319 — Barel — 2026-03-15T07:20:38+00:00

URL: https://externals.io/message/130319#130319

Hi all,

I would like to create an RFC proposal suggesting modifying the
str_contains, str_starts_with and str_ends_with functions so that they can
accept an array argument for the $needle parameter (apart from accepting a
string as they do now). If an array is passed and the $haystack string
contains, starts with or ends with any of the strings in the array then the
functions will return true, otherwise returns false.

I looked at the RFCs where these functions were added and at the related
internal discussions and could not find any discussion about this
possibility.
Before creating the RFC I would like to hear the thoughts of the group,
many thanks in advance

Cheers

Carlos

---

## #130320 — Derick Rethans — 2026-03-15T08:20:53+00:00

URL: https://externals.io/message/130319#130320 · In reply to: #130319

>Hi all,
>
>I would like to create an RFC proposal suggesting modifying the
>str_contains, str_starts_with and str_ends_with functions so that they can
>accept an array argument for the $needle parameter (apart from accepting a
>string as they do now). If an array is passed and the $haystack string
>contains, starts with or ends with any of the strings in the array then the
>functions will return true, otherwise returns false.
>
>I looked at the RFCs where these functions were added and at the related
>internal discussions and could not find any discussion about this
>possibility.
>Before creating the RFC I would like to hear the thoughts of the group,
>many thanks in advance
>
>Cheers
>
>Carlos

The semantics for starts_with and ends_with are easy to argue they should mean any element, but for str_contains it could be useful to have either all, or any.

I'm therefore thinking it might be better to have separate functions to make this clear.

cheers
Derick

---

## #130321 — Barel — 2026-03-15T09:14:49+00:00

URL: https://externals.io/message/130319#130321 · In reply to: #130320

> 
> > […]
> the
> > […]
>
> The semantics for starts_with and ends_with are easy to argue they should
> mean any element, but for str_contains it could be useful to have either
> all, or any.
>
> I'm therefore thinking it might be better to have separate functions to
> make this clear.
>
> cheers
> Derick


Thanks Derick,

While “contains all” semantics can be useful in some niche scenarios, I
believe they are much less frequent than the "contains any" semantics. And
“contains any” is the direct parallel to the proposed behavior for
 `str_starts_with()` and `str_ends_with()`, keeping the mental model consistent
across all three functions. Would like to hear the thoughts of other people
about this issue

Cheers

Carlos

---

## #130361 — Morgan — 2026-03-16T00:41:30+00:00

URL: https://externals.io/message/130319#130361 · In reply to: #130319

> Hi all,
> 
> I would like to create an RFC proposal suggesting modifying the | 
> str_contains, |str_starts_with and str_ends_with functions so that they 
> can accept an array argument for the |$needle| parameter (apart from 
> accepting a string as they do now). If an array is passed and the | 
> $haystack| string contains, starts with or ends with any of the strings 
> in the array then the functions will return true, otherwise returns false.
> 
> I looked at the RFCs where these functions were added and at the related 
> internal discussions and could not find any discussion about this 
> possibility.
> Before creating the RFC I would like to hear the thoughts of the group, 
> many thanks in advance
> 
> Cheers
> 
> Carlos

The functions were added after it was pointed out that userland 
implementations tended to result in the information being derived 
internally and then thrown away, requiring the user to reconstruct it; 
it made sense to yield that internal information directly.

For this extension, I'm not sure what the advantages are over simple 
function composition (array_any was introduced for exactly this sort of 
job and is applicable to any function). Or disadvantages (e.g. more 
complex function signature).

---

## #130363 — tim@bastelstu.be — 2026-03-16T14:30:11+00:00

URL: https://externals.io/message/130319#130363 · In reply to: #130361

Hi

Am 2026-03-16 01:41, schrieb Morgan:
> For this extension, I'm not sure what the advantages are over simple 
> function composition (array_any was introduced for exactly this sort of 
> job and is applicable to any function)

I agree. Especially since the Partial Function Application RFC was 
accepted an ad-hoc implementation would just be:

     $str = 'bazinga';

     $strStartsWithAny = \array_any(['foo', 'bar', 'baz'], 
\str_starts_with($str, ?));

I have also added an optimization to PHP 8.6 that compiles `array_map()` 
into a foreach loop when the callback is first class callable or PFA: 
https://github.com/php/php-src/pull/20934. I plan to extend this to the 
other array_* functions once the PFA PR is merged.

Best regards
Tim Düsterhus

---

## #130366 — Barel — 2026-03-16T16:51:25+00:00

URL: https://externals.io/message/130319#130366 · In reply to: #130363

> Hi
>
> Am 2026-03-16 01:41, schrieb Morgan:
> > […]
>
> I agree. Especially since the Partial Function Application RFC was
> accepted an ad-hoc implementation would just be:
>
>      $str = 'bazinga';
>
>      $strStartsWithAny = \array_any(['foo', 'bar', 'baz'],
> \str_starts_with($str, ?));
>
> I have also added an optimization to PHP 8.6 that compiles `array_map()`
> into a foreach loop when the callback is first class callable or PFA:
> https://github.com/php/php-src/pull/20934. I plan to extend this to the
> other array_* functions once the PFA PR is merged.
>
> Best regards
> Tim Düsterhus
>

Tim, Morgan

Thanks for your comments. I did not realise that this would be easily
implemented by using `array_any`. This means that the RFC that I was
planning to create is not actually needed

Cheers

Carlos

---

## #130378 — Larry Garfield — 2026-03-16T22:07:08+00:00

URL: https://externals.io/message/130319#130378 · In reply to: #130363

> I have also added an optimization to PHP 8.6 that compiles `array_map()` 
> into a foreach loop when the callback is first class callable or PFA: 
> https://github.com/php/php-src/pull/20934. I plan to extend this to the 
> other array_* functions once the PFA PR is merged.
>
> Best regards
> Tim Düsterhus

!!! I didn't realize you'd done so.  That's awesome, and needs more publicity. :-)

With all the compile optimizations around pipes, PFA, etc., I wonder if there's a way to make it more generic?  Like, can tools be built for pipes and PFA that can get down-compiled, without having to special case everything in the engine?

--Larry Garfield
