Bug #75130 [Com]: array_filter pass-by-ref broken

From: Date: Mon, 28 Aug 2017 13:51:35 +0000
Subject: Bug #75130 [Com]: array_filter pass-by-ref broken
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-210853@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75130&edit=1 ID: 75130 Comment by: spam2 at rhsoft dot net Reported by: kernins at gmail dot com Summary: array_filter pass-by-ref broken Status: Not a bug Type: Bug Package: Arrays related Operating System: Linux x86_64 PHP Version: 7.1.8 Block user comment: N Private report: N New Comment: http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html Previous Comments: ------------------------------------------------------------------------ [2017-08-28 13:49:20] kernins at gmail dot com First of all, it worked the whole 5.x series and I have a lot of code using it that way. Such usage was considered valid before => BC break here. 2nd >The correct way to write this code is a combination of array_map and array_filter. This will result in two loops instead of one => performance hit on large arrays (even if it is negligible, it is still here) and less compact code. 3rd Why the hell one array_* functions are working with refs, while the others shouldn't? This at least adds even more inconsistency to already inconsistent php api. So, this IS a bug ------------------------------------------------------------------------ [2017-08-28 12:27:42] nikic@php.net If a function doesn't explicitly document that it's operating on references (such as array_walk), then you can generally assume that it does not work with references. The correct way to write this code is a combination of array_map and array_filter. ------------------------------------------------------------------------ [2017-08-28 12:11:52] kernins at gmail dot com Description: ------------ Passing array element to callback by ref doesn't work anymore, resulting array contains original/unmodified values Test script: --------------- $arr=['foo', 'bar', '', 'baz']; $arr=array_filter($arr, function(&$el){ $el=strtoupper($el); return strlen($el)>0; }); var_dump($arr); Expected result: ---------------- array(3) { [0]=> string(3) "FOO" [1]=> string(3) "BAR" [3]=> string(3) "BAZ" } Actual result: -------------- array(3) { [0]=> string(3) "foo" [1]=> string(3) "bar" [3]=> string(3) "baz" } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75130&edit=1

« previous php.bugs (#210853) next »