|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81417 Option to determine if reading an undefined array key should throw a warning
Submitted: 2021-09-05 12:20 UTC Modified: 2021-09-06 01:50 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: nicolas at gestixi dot com Assigned:
Status: Wont fix Package: *Configuration Issues
PHP Version: 8.0.10 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: nicolas at gestixi dot com
New email:
PHP Version: OS:


 [2021-09-05 12:20 UTC] nicolas at gestixi dot com
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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-09-05 12:22 UTC] nicolas at gestixi dot com
-Summary: Add a configuration option to determine if reading an undefined array key shoul +Summary: Option to determine if reading an undefined array key should throw a warning
 [2021-09-05 12:22 UTC] nicolas at gestixi dot com
Adjust the summary which was too long.
 [2021-09-05 14:39 UTC] php-bugs at allenjb dot me dot uk
You can already silence this warning in your own code using a custom error handler. See

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):

In the case you gave, you would write: if ($array['key'] ?? false)
 [2021-09-05 18:41 UTC] nicolas at gestixi dot com
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.
 [2021-09-06 01:50 UTC]
-Status: Open +Status: Wont fix
 [2021-09-06 01:50 UTC]
As a rule of thumb, adding another php.ini option is not the right answer. What's more, adding one that tells PHP to ignore certain types of warnings/errors is opening a very big and very dangerous door that really ought to remain closed.

If your codebase is 10 years old then you'll have to accept that you cannot simply upgrade PHP versions (especially majors) and expect everything to work as it did back in 2011. PHP 7.4 is the last of the 7.x series and will remain in active support for an extended period - use that time to upgrade your codebase to be compatible with 8.x.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Oct 28 18:04:14 2021 UTC