Just wanted to follow up with the real-world data I promised for array_str_contains().
I scanned the top 200 Composer packages (around 21,000 PHP files) to see how people currently handle array substring searches. Here is what I found (32 matches total):
array_filter with str_contains: 8
array_filter with strpos: 8
array_filter with stripos: 6
foreach with str_contains: 6
array_filter with preg_match: 4
All these different (and sometimes verbose) approaches can simply be replaced with a single, clean, and fast native call:
array_str_contains($haystack, $needle)
On 3 September 2026 17:18:14 BST, "سپهر محمودی" <sepehrphpr@gmail.com> wrote:
- array_filter with str_contains: 8
- array_filter with strpos: 8
- array_filter with stripos: 6
- foreach with str_contains: 6
- array_filter with preg_match: 4
All these different (and sometimes verbose) approaches can simply be
replaced with a single, clean, and fast native call:
array_str_contains($haystack, $needle)
I just went through the results, and found at least 15 of these which are doing extra logic that couldn't be replaced with the proposed function. There are probably more I missed, or where the extra logic isn't obvious because of the way the code is truncated in the report.
So we're looking at maybe 15 uses in 200 packages. That's still something, but it's not strong evidence that this is an extremely common task.
All these different (and sometimes verbose) approaches can simply be
replaced with a single, clean, and fast native call:
array_str_contains($haystack, $needle)
I just went through the results, and found at least 15 of these which are doing extra logic that couldn’t be replaced with the proposed function. There are probably more I missed, or where the extra logic isn’t obvious because of the way the code is truncated in the report.
So we’re looking at maybe 15 uses in 200 packages. That’s still something, but it’s not strong evidence that this is an extremely common task.
Regards,
Rowan Tommins
[IMSoP]
Hi Rowan,
Thanks for actually going through the results, that’s exactly the kind of
review I was hoping for.
You’re right, the scanner only detects the pattern combination, not the
full body of the closure. So those numbers represent an upper bound, and
some matches do include extra logic. I’ll update the RFC to be explicit
about this instead of implying all 32 are direct replacements.
I’ll also try to refine the scan to flag matches that look like a pure
substring check, and report both numbers (strict vs. loose matches) so
we can see the real lower bound too.
Just wanted to follow up with the real-world data I promised for array_str_contains().
I scanned the top 200 Composer packages (around 21,000 PHP files) to see how people currently handle array substring searches. Here is what I found (32 matches total):
- array_filter with str_contains: 8
- array_filter with strpos: 8
- array_filter with stripos: 6
- foreach with str_contains: 6
- array_filter with preg_match: 4
All these different (and sometimes verbose) approaches can simply be replaced with a single, clean, and fast native call:
array_str_contains($haystack, $needle)
Just wanted to follow up with the real-world data I promised for array_str_contains().
I scanned the top 200 Composer packages (around 21,000 PHP files) to see how people currently handle array substring searches. Here is what I found (32 matches total):
array_filter with str_contains: 8
array_filter with strpos: 8
array_filter with stripos: 6
foreach with str_contains: 6
array_filter with preg_match: 4
All these different (and sometimes verbose) approaches can simply be replaced with a single, clean, and fast native call:
array_str_contains($haystack, $needle)
“Non-string values in the array are implicitly cast to strings before
the check”.
Is actually the case implementation wise ?
Hi David,
Yes, non-string scalar values (like integers or floats) can be cast/treated as strings during the search (similar to how some string-matching functions handle scalar inputs), or skipped if not convertible.
I will make sure the wording in the RFC clearly explains how non-string values within the array are handled.
در تاریخ جمعه ۴ سپتامبر ۲۰۲۶، ۲۱:۱۳ David CARLIER <devnexen@gmail.com> نوشت:
Hi,
On Thu, 3 Sept 2026 at 17:21, سپهر محمودی <sepehrphpr@gmail.com> wrote:
>
> Hey internals,
>
> Just wanted to follow up with the real-world data I promised for array_str_contains().
>
> I scanned the top 200 Composer packages (around 21,000 PHP files) to see how people currently handle array substring searches. Here is what I found (32 matches total):
>
> - array_filter with str_contains: 8
> - array_filter with strpos: 8
> - array_filter with stripos: 6
> - foreach with str_contains: 6
> - array_filter with preg_match: 4
>
> All these different (and sometimes verbose) approaches can simply be replaced with a single, clean, and fast native call:
> array_str_contains($haystack, $needle)
>
> I’ve updated the RFC with these findings:
> PHP: rfc:array_str_contains
>
> If you want to check out the script or raw JSON results, I uploaded them here:
> Packagist static analysis report: patterns of array filtering with string contains (for array_str_contains RFC٫ · GitHub
>
> Let me know what you think!
>
> Cheers,
> Sepehr
>
quick look in the RFC about this
"Non-string values in the array are implicitly cast to strings before
the check".
Is actually the case implementation wise ?
--------
Hi David,
Yes, non-string scalar values (like integers or floats) can be cast/treated as strings during the search (similar to how some string-matching functions handle scalar inputs), or skipped if not convertible.
I will make sure the wording in the RFC clearly explains how non-string values within the array are handled.
Best regards,
Sepehr
you probably need to update your test to demonstrate it.