Req #81417 [Opn]: Option to determine if reading an undefined array key should throw a warning

From: Date: Sun, 05 Sep 2021 18:41:06 +0000
Subject: Req #81417 [Opn]: Option to determine if reading an undefined array key should throw a warning
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-236425@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81417&edit=1 ID: 81417 User updated by: nicolas at gestixi dot com Reported by: nicolas at gestixi dot com Summary: Option to determine if reading an undefined array key should throw a warning Status: Open Type: Feature/Change Request Package: *Configuration Issues PHP Version: 8.0.10 Block user comment: N Private report: N New Comment: I tried with a custom error handler. Something like: if (strpos($errstr, 'Undefined index:') !== false OR strpos($errstr, 'Undefined offset:') !== false) return true; else return false; But it exhausts the memory before the end of the request. Previous Comments: ------------------------------------------------------------------------ [2021-09-05 14:39:51] php-bugs at allenjb dot me dot uk You can already silence this warning in your own code using a custom error handler. See https://www.php.net/set_error_handler You should however keep in mind that warnings tend to get escalated to errors over time (this was a notice since at least 5.4) The null coalescing operator also provides an alternative "fix" that you may find more readable - like isset() it silences warnings about undefined indexes (and properties or variables): https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op In the case you gave, you would write: if ($array['key'] ?? false) ------------------------------------------------------------------------ [2021-09-05 12:22:40] nicolas at gestixi dot com Adjust the summary which was too long. ------------------------------------------------------------------------ [2021-09-05 12:20:50] nicolas at gestixi dot com Description: ------------ In some languages, such as JavaScript, accessing an undefined array key can be performed silently without throwing an error. We have a 10 years old code base which does conditions like this everywhere: if ($array['key']). With PHP 8, this coding style throws a warning if the key is not defined. Replacing it by if (array_key_exists('key', $array) && $array['key']) would take us weeks... and, I actually prefer our actual syntax which I found more readable. For these reasons, I do not plan to upgrade all our code base to avoid this warning. And I do not want to ignore all warnings just for this one... Would it be possible to add a configuration setting to determine if this coding style should throw a warning, a notice or be silently ignored? Another solution I see would be to be able to ignore specific warnings/notices. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=81417&edit=1

« previous php.bugs (#236425) next »