So, looks like they changed the way accessing an array with an unknown key raises a message.
<?php
if($myArray['foo']) { ... }
For 25 years this was simply raising a NOTICE, and people were quite happy to silence E_NOTICE
in php.ini
. With (I think) PhP 8.0 this raises now a WARNING.
For obvious reason I don't want to silence E_WARNING
, so I (and all the rest of the world who for years used uninitialized variables as their value was simply null
, like in so many other interpreted language) was looking for a possible way to get rid of warnings related to undefined variables/arrays/keys while keep reported all the other (more serious) programming error, like including a non existing file.
Reason behind this question is that I have to deal with tons of code written with above pattern in mind; I just can't rewrite it all, but still I need to switch to PhP 8, so no, I'm not asking how to use isset()
.
You can call the set_error_handler
function and define a callback that bypasses that specific warning. When the callback returns true it won't trigger php error handling and will do nothing, in all other cases (return false) it will use the default error handling. See more here: https://www.php.net/manual/en/function.set-error-handler.php
set_error_handler(function(int $errno, string $errstr) {
if ((strpos($errstr, 'Undefined array key') === false) && (strpos($errstr, 'Undefined variable') === false)) {
return false;
} else {
return true;
}
}, E_WARNING);
Be careful that some frameworks will call set_error_handler themselves and you can only have one error handler callback so it might always not work