Bug #74038 [Dup]: Arrays unchanged when value reference is modified in an array_filter(), etc

From: Date: Fri, 03 Feb 2017 06:52:58 +0000
Subject: Bug #74038 [Dup]: Arrays unchanged when value reference is modified in an array_filter(), etc
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207144@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74038&edit=1 ID: 74038 Updated by: requinix@php.net Reported by: Rican7 at gmail dot com Summary: Arrays unchanged when value reference is modified in an array_filter(), etc Status: Duplicate Type: Bug Package: Arrays related PHP Version: 7.0.15 Block user comment: N Private report: N New Comment: No, references in general are well defined. Functions that say they support references do, functions that don't say they support references don't (probably). Imagine you were writing array_filter() using PHP code: references might work in one version (https://3v4l.org/YDZXk) but then you revise the code and now references don't work (https://3v4l.org/7OgqC). array_filter_() never claimed it worked with references. There's no documentation to suggest it does. But for a while it did. Then the implementation changed and now it doesn't. It's not so much a matter of references working before but rather you being able to get away with it. Previous Comments: ------------------------------------------------------------------------ [2017-02-03 06:16:32] Rican7 at gmail dot com Ah, so taking a value by reference in the callback is really just "undefined" behavior that shouldn't be relied on anyway? ------------------------------------------------------------------------ [2017-02-03 05:30:53] requinix@php.net Yeah, same basic issue. Those functions never claimed you could modify the values inside the callbacks, so the fact that you could in PHP 5 was a quirk. ------------------------------------------------------------------------ [2017-02-03 02:24:17] Rican7 at gmail dot com Ah, this seems to be related to (if not the same issue as) #71240: https://bugs.php.net/bug.php?id=71240 ------------------------------------------------------------------------ [2017-02-03 01:51:52] Rican7 at gmail dot com Description: ------------ I just updated an application from PHP 5.6 to PHP 7.0 after carefully reading the upgrading guide and documentation, but I just ran into a bit of an issue. We had code that was modifying values (via a value reference) in place when passed to an array_filter() call, to prevent us from having to run multiple loop iterations, but that doesn't seem to work in PHP 7. While I understand that the runtime semantics may have changed for these functions, I can't find this behavior change to be documented anywhere, so I wasn't sure if it was intentional or not. I know that foreach has changed in PHP 7, which is relatively well documented, but these array function behavior changes don't seem to be. I've tried to see if this behavior has changed in other array functions too, such as array_reduce, and the issue seems to exist there too. My guess is that this has to do with how values are passed/referenced in these functions under the new PHP 7 runtime/engine. Test script: --------------- /** * 3v4l.org URL: https://3v4l.org/bERrp */ $data = ['foo', 'bar']; var_dump($data); $filtered = array_filter($data, function (&$value) { $value = (bool) $value; return true; }); var_dump($filtered); Expected result: ---------------- array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } array(2) { [0]=> bool(true) [1]=> bool(true) } Actual result: -------------- array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74038&edit=1

« previous php.bugs (#207144) next »