-1

I already use:

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

at the beginning of my code. However, the error messages are displayed directly in the output (which is a lot, so you can't find the errors well).

How do I display all the errors that occurred, after the script is done loading?

user2342558
  • 5,567
  • 5
  • 33
  • 54
Tubica
  • 11
  • 2
  • 1
    Log them to a file, and just show a generic error page to the user. https://phpdelusions.net/articles/error_reporting has a sensible guide to setting up your error reports. – ADyson Sep 01 '23 at 09:58
  • 1
    PHP has no direct mechanism for that. You could suppress the display in the output, and check the error log instead. Or you could implement your own custom error handler, then you are in full control of what you output when & where. And then there's some external solutions, that come in the form of a browser extension (but still need some server-side component to be installed as well), https://stackoverflow.com/a/39210214/1427878 – CBroe Sep 01 '23 at 09:58
  • Your code shoudn't have any error. The only best approach is to solve all of them everytime they happens. – user2342558 Sep 01 '23 at 10:35
  • @user2342558 that's why I am trying to get all the errors. TO FIX THEM! But its hard to fix errors, you can't even see ;) – Tubica Sep 01 '23 at 11:36
  • I don't understand what is your problem with "finding" errors. Just open the page source, get to very first error message, get to the code, fix the error, reload the page, get to the next error and so on – Your Common Sense Sep 01 '23 at 11:53
  • @YourCommonSense bruh. The script is very complicated with a lot of loops. It needs 3 hours to go through it once. There are 13000 files it has to process and 30 Million outputs. You can't "just open the page". It takes time and I just want the errors in one place after it is done. It's like one error every a million outputs and I can't stare at the output for hours straight. – Tubica Sep 01 '23 at 12:27
  • What you just said makes no sense. While fixing errors, you should work on a single file with ten records. And process your 30 million **only after fixing errors** – Your Common Sense Sep 01 '23 at 12:29
  • @YourCommonSense you have no Idea my man. Just accept, that I need to get the errors. I do not know which files have anything in them that leads to an error. I need to go through all of them. It is not possible otherwise. Don't pretend like you know everything. I know what I am doing, I just need help. You are no help. You're just being a smartass.. – Tubica Sep 01 '23 at 12:41
  • In case it's production environment, you must log errors instead of displaying them, as you have been told already – Your Common Sense Sep 01 '23 at 12:42

1 Answers1

0

You can use error / exception handler along with a destructor. The handlers capture errors and the destructor will display captured errors at the very bottom of the page.

class ErrorHandler {
    private static $instance = null;
    private static $errors = [];
    
    public static function Init() {
        if(!self::$instance) {
            self::$instance = new self;
            
            set_error_handler(function($errno, $errstr, $errfile, $errline){
                self::$errors[] = new ErrorException($errstr, 0, $errno, $errfile, $errline);
            });
            
            set_exception_handler(function($ex){
                self::$errors[] = $ex;
            });
        }
    }
    
    function __destruct(){
        foreach(self::$errors as $errstr)
            echo $errstr, '<br />';
    }
}

ErrorHandler::Init();
shingo
  • 18,436
  • 5
  • 23
  • 42
  • That's interesting solution but wrong answer to this particular question. This is essentially a *temporary* situation, and it must be fixed by infrastructure, not the code. – Your Common Sense Sep 01 '23 at 11:48
  • This is a common method I use in development, sometimes errors are displayed in inconspicuous places. I think this requirement itself is temporary, as you should expect all errors to be fixed before going live. – shingo Sep 01 '23 at 12:01
  • @shingo thank's for taking me serious! This guy is so ignorant.. Of course I only need the errors to FIX THEM, but I can't fix them without even finding them xD – Tubica Sep 01 '23 at 12:44
  • @Tubica no, if you really have tons of errors you'd better use error log, since outputing them still takes time. – shingo Sep 01 '23 at 13:00