4

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

Jack
  • 1,488
  • 11
  • 21
  • 2
    I don't think so. There's no way to filter by specific types of messages, you just have the different error categories. – Barmar Dec 10 '21 at 21:39
  • 3
    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. – mickmackusa Dec 10 '21 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 );` – berend Dec 10 '21 at 21:45
  • 1
    @berend please reread the question title. Can I hide PHP Warning: Undefined array key **without suppressing all other warnings**? – mickmackusa Dec 10 '21 at 21:48
  • 4
    The lesson is that you shouldn't have been ignoring the notices all this time either. – Sammitch Dec 10 '21 at 21:52
  • I am struggling to not hammer this question with: https://stackoverflow.com/q/5061432/2943403, https://stackoverflow.com/q/4261133/2943403, https://stackoverflow.com/q/1195549/2943403. Equally, tempting is posting a two letter-answer: `No`. – mickmackusa Dec 11 '21 at 02:56

2 Answers2

8

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

valepu
  • 3,136
  • 7
  • 36
  • 67
bernd
  • 104
  • 1
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 15 '22 at 13:45
-1

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.

Demis Palma ツ
  • 7,669
  • 1
  • 23
  • 28
  • 1
    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 Feb 14 '22 at 17:16
  • @Jack yes, not testing the variable existence is an option, but you have to live with the consequences, which means notices or warnings. – Demis Palma ツ Feb 14 '22 at 21:19
  • 1
    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](https://www.php.net/manual/en/language.variables.basics.php]manual) for years states that **It is not necessary to initialize variables**. It is just another PhP result of its amateur design. – Jack Feb 15 '22 at 10:22
  • 2
    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 – Jack Feb 15 '22 at 10:26
  • I can't believe this answer has been written in 2022 – Your Common Sense Jan 05 '23 at 11:32
  • it should be `if($myArray['foo'] ?? false) { ... }` instead of `@` suppression – vp_arth Mar 14 '23 at 21:44