|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2021-11-17 23:16 UTC] requinix@php.net
-Status: Open
+Status: Not a bug
[2021-11-17 23:16 UTC] requinix@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 23 20:00:01 2025 UTC |
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.