Bug #81633 [Opn->Nab]: Passing array by reference to function sets key

From: Date: Wed, 17 Nov 2021 23:16:36 +0000
Subject: Bug #81633 [Opn->Nab]: Passing array by reference to function sets key
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237833@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81633&edit=1 ID: 81633 Updated by: requinix@php.net Reported by: ogdg at protonmail dot com Summary: Passing array by reference to function sets key -Status: Open +Status: Not a bug Type: Bug Package: Arrays related Operating System: Fedora 35 PHP Version: 8.0.12 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php That is not how references are designed to work. https://www.php.net/manual/en/language.references.php Previous Comments: ------------------------------------------------------------------------ [2021-11-17 23:00:24] ogdg at protonmail dot com Description: ------------ For any user-defined function where the argument is passed by reference, and an array is passed with reference to an undefined key as the argument for the function, this causes the key to be set in the array. Test script: --------------- function test(&$a) { return; } If you have an empty array: $b = array(); And do this: test($b[5]); The array $b will now have a key '5' set. Expected result: ---------------- This should not happen, but it does and it it affects PHP 7 as well, possibly other versions. The variable/array passed as the function argument should not be altered or set unless the function itself deliberately makes changes. I cannot think of any good reason that a value should be set merely by passing it as a function argument. The reason I discovered this is that PHP 8 now throws warnings for "undefined array key", and to mitigate this I changed several functions to allow the arguments to be passed by reference. While this was a notice on older versions of PHP, it should be completely removed from the "error reporting" of PHP and/or made into a debugging feature that is disabled by default. Many scripts check for the existence of a key in an array and if the key does not exist then no special message should be generated. There is no reason that is_array() should throw an "undefined key" warning; if the key isn't there then any checks for its existence should simply respond logically, i.e. false or null. I suspect that this has been the cause of many PHP applications suffering from mysterious errors or other unexpected behaviors, so I hope that we can fix this without cumbersome "workarounds" like using the null coalescing operator. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=81633&edit=1

« previous php.bugs (#237833) next »