skip to Main Content

Since upgrading from php 5 to 8, I’ve started getting a lot of "Warning: Trying to access array offset on value of type null" when using mysqli_fetch_array to run queries which may or may not return any results (it’s fine if they don’t!).

I get why this is happening, and have seen various threads recommending to just test for a result before trying to use it, which is great for individual instances. But I’ve got a whole lot of pages with this issue, and probably plenty I’ve not found yet. So what I’m looking for is a way to stop the error en-masse, rather than editing each page.

The obvious way to do this would seem to be if I could just disable that specific warning… not sure if that’s possible though?

2

Answers


  1. You can define a custom error handler to intercept that specific warning and ignore it.

    set_error_handler(
        static fn (int $errno, string $errstr): bool =>
            $errstr === 'Trying to access array offset on value of type null'
    );
    

    Custom error handlers should return true if they’ve handled the error and false if the error should be passed on to the native error handler. This handler basically just pretends to handle that very specific warning and passes everything else off to the default handler.

    Working example: https://3v4l.org/SgZ7f0


    EDIT: Just to be clear: this is not a permanent solution. Where there’s a will, there’s a way, but this way madness lies.

    The only possible use case for something like this is the exact use case OP has: when upgrading from PHP 5 to PHP 8 you will be bombarded with errors, warnings and notices. If a specific warning occurs hundreds or thousands of times, you might want to temporarily hide it so you can focus on fixing other, more important warnings first.

    That doesn’t mean you shouldn’t fix this: it is a WARNING-level event for a reason. Your code is probably behaving in unexpected ways and this might become an ERROR-level event in future PHP versions.

    Keeping this error handler to hide the warning means you’re no longer aware of the problem, and an even bigger dragon will bite you when you upgrade to a newer PHP version where this situation is now an error.

    In the best case, this error handler is only implemented on the production environment so that its hard drive doesn’t get filled with warning logs, while on the development environment this error handler does not exist so that the developer is confronted with these warnings as a reminder to fix the issue.

    Login or Signup to reply.
  2. Don’t ignore errors. PHP is being helpful by throwing the warnings. You should treat every warning as an error or coding mistake. They are not to be ignored.

    Recent PHP versions improved error reporting. If you only learned about these mistakes now, it’s time to fix them before you release a new version of your product. If you cannot find all places where these warnings occur, monitor the error log for some time and fix all instances of this warning appearing.

    Do not seek ways to disable the warnings!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search