Setting the directive display_errors
to true
(while having error_reporting
set to E_ALL
) prints all errors that occured during the current request bofore returing the PHP output.
Since I'm sending headers in my PHP code, I get several more errors (sending header after content has been sent is not possible).
Now I'd like to add the error messages to the end of my page. There I'd like to show all errors that occurred (until then). Unfortunately error_get_last only returns the last error that occurred.
I first thought that set_error_handler might solve the problem, but I'm afraid that my error-logging does not work anymore:
It is important to remember that the standard PHP error handler is completely bypassed for the error types specified by error_types unless the callback function returns FALSE.
Besides that:
The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.
But maybe they are also not available in error_get_last()
So is there a way to print all errors after the output of the generated content?
There is a discussion about this here and here (amongst others), with the second answer on the latter list using a mix of
set_error_handler()
withregister_shutdown_function()
being a good solution.Fuller example that catches all the errors (exceptions and errors), pumps to a log file and remembers in a static class; at the end you query the static class to get the error.
I use similar to this for all projects to get full control over all errors.
Usage Example
Referring to your first concern
The error handling will still work as long as the:
One solution to store all error-numbers (in an outside array
$error_list
) could be:Another approach is to use Exceptions. There is a nice example in the comments for the function set_error_handler()
Also see:
Referring to your second concern:
As
Egg
already mentioned: usingregister_shutdown_function()
is a way to go, and the code in the answer https://stackoverflow.com/a/7313887/771077 shows you how.But keep in mind, that
Catch the errors as you go and output messages at the end.