Bug #79850 [Opn]: array_walk / array_filter incorrectly warn variables should be passed by ref
| From: | sjon@php.net | Date: | Tue, 14 Jul 2020 12:42:41 +0000 |
| Subject: | Bug #79850 [Opn]: array_walk / array_filter incorrectly warn variables should be passed by ref | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-228035@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79850&edit=1
ID: 79850
Updated by: sjon@php.net
Reported by: sjon@php.net
Summary: array_walk / array_filter incorrectly warn variables
should be passed by ref
Status: Open
Type: Bug
Package: Arrays related
Operating System: archLinux
PHP Version: 8.0.0alpha2
Block user comment: N
Private report: N
New Comment:
Aha, so this fix would be for the user to write
```
$newIds = array_filter($ids, function($n) {
return intval($n);
});
```
or in this case, simply
$newIds = array_filter($ids, 'intval');.
Does the same reasoning apply to https://3v4l.org/ZCgRe ? It
seems internal functions that accept references will function correctly but still throw the same
warning.
Previous Comments:
------------------------------------------------------------------------
[2020-07-13 12:54:46] nikic@php.net
This is an intentional change, now documented with https://github.com/php/php-src/commit/2edc5e00ac5ceef6429c3ef4651745efa97a13d8.
As the the inverted message, I can see where you're coming from there... The message appears
because array_filter() internally calls the callback with a value, but the function expects a
reference, thus the warning in this form.
------------------------------------------------------------------------
[2020-07-13 12:21:33] sjon@php.net
Description:
------------
I'm not 100% sure this is a bug but both array_walk and array filter throw warnings when
variables are passed by reference. Either the message is inverted (they don't accept variables
by reference) or it's incorrectly thrown
Originally found as https://3v4l.org/WH94Q
Test script:
---------------
$ids = array("8479");
$newIds = array_filter($ids, function(&$n) {
$n = intVal($n);
});
Expected result:
----------------
no warning
Actual result:
--------------
Warning: {closure}(): Argument #1 ($n) must be passed by reference, value given in /in/WH94Q on line
7
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79850&edit=1