0

Is it possible to capture/gather all the errors on page and concatenate them to a string by using:

$allErrors = ""; 
$allErrors .= error_get_last(); // Each time an error shows up

I like to log errors in my database and would prefer to log all these PHP errors since I already have SQL-related PHP Fatal Errors logged.

theflarenet
  • 642
  • 2
  • 13
  • 27
  • looking for [`implode`](http://php.net/manual/en/function.implode.php) maybe? Or suppose `print_r($a, true)`. Honestly not an advocate of what you are proposing. Assume some custom error handler ([`set_error_handler`](http://php.net/manual/en/function.set-error-handler.php)) is doing this? – ficuscr May 11 '18 at 19:35
  • My main goal is to log all PHP errors from `ini_set('display_errors', 1)` into my database but I'm not sure how to gather all of them when `error_get_last()` only gets me the last error. – theflarenet May 11 '18 at 19:38
  • 1
    Take a look at this one maybe: https://stackoverflow.com/questions/36425156/get-all-php-errors-warnings-that-occurred-during-the-current-request – ficuscr May 11 '18 at 19:39

1 Answers1

1

error_get_last(), like the name is suggesting, only gives you the last error. And the fact that most errors will stop your script from running will get you only the last one and none of the previous ones. But you can set an own handler to catch every thrown error and exception. Here is an example

//function for exception handling
function handle_exception (Exception $exception) {

    //here you can save the exception to your database

}

//function for error handling
function handle_error ($number, $message, $file, $line, $context = null) {

    //pass/throw error to handle_exception
    throw new ErrorException ($message, 0, $number, $file, $line);

}

//set error-handler but only for E_USER_ERROR and E_RECOVERABLE_ERROR
set_error_handler ('handle_error', E_USER_ERROR|E_RECOVERABLE_ERROR);

//exception-handler
set_exception_handler ('handle_exception');
wayneOS
  • 1,427
  • 1
  • 14
  • 20
  • I figured it out that I had to replace `E_USER_ERROR|E_RECOVERABLE_ERROR` with `E_WARNING`. However, I'd like to keep the page displaying instead of it completely stopping the script. How is this possible? – theflarenet May 11 '18 at 20:10
  • @theflarenet it wont stop unless the error/exception is preventing the script from finishing. you can use `echo $exception -> getMessage();` to display the error-messages – wayneOS May 11 '18 at 20:28
  • I believe `throw new ErrorException ($message, 0, $number, $file, $line);` is the line where it prevents the script from finishing. Removing it runs the rest of the script as it still displays errors. – theflarenet May 11 '18 at 20:36
  • @theflarenet ah ok. you are right. thats wyh i just catched E_USER_ERROR and E_RECOVERABLE_ERROR. because those two are the real important ones and they will also stop your script. therefore throwing an exception didnt matter. but you can also just save the error to your db in `function handle_error` – wayneOS May 11 '18 at 20:57