7

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().

15
  • 2
    I don't think so. There's no way to filter by specific types of messages, you just have the different error categories. Commented Dec 10, 2021 at 21:39
  • 4
    You might not be asking how to use isset(), but it is certainly one of the ways to professionally resolve your XY Problem. PHP8 is helping developers to stop writing bad code for another 25 years. Commented Dec 10, 2021 at 21:45
  • 3
    maybe consider not showing ini_set('display_errors', 'Off');, but logging errors. ini_set('log_errors', 'On');. log all: ini_set('error_reporting', E_ALL ); Commented Dec 10, 2021 at 21:45
  • 1
    @berend please reread the question title. Can I hide PHP Warning: Undefined array key without suppressing all other warnings? Commented Dec 10, 2021 at 21:48
  • 4
    The lesson is that you shouldn't have been ignoring the notices all this time either. Commented Dec 10, 2021 at 21:52

2 Answers 2

17

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

Sign up to request clarification or add additional context in comments.

Comments

0

Use the Error Suppression operator @

<?php
if(@$myArray['foo']) { ... }

It's not as good as isset(), but at least it clearly says to the reader that you expect invalid indexes now and then.

Time has come to pay off your technical debt.

7 Comments

Who says that accessing an undefined variable in PhP < 8 is a tech debt? Not testing the variable existence is a choice, if the language permits so.
@Jack yes, not testing the variable existence is an option, but you have to live with the consequences, which means notices or warnings.
So why judging other's work as technical debt? And for reference, it's my opinion that you shouldn't face any consequence when the manual for years states that It is not necessary to initialize variables. It is just another PhP result of its amateur design.
Also please note that reading an undefined variable is perfectly allowed in many script languages; can you image the next version of bash requiring you to declare variables before their usage? Half computers of the world wouldn't boot anymore
it should be if($myArray['foo'] ?? false) { ... } instead of @ suppression
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.