If your website catches an exception that you want reported back to the users as feedback, what is an elegant way to handle it without running into these problems:
The error happened on the middle of the page, and if you directly print the error message when caught it will be sitting somewhere in the middle of a broken page.
Using ob_start() can make sure you only print content after it has been processed and there are no errors, fixing the issue at 1), but it carries a performance hit that would best be avoided.
Redirecting to a new URL or the same one after catching an error can work really well (like redirecting back to a login form if the user posted the wrong password, with an error messsage, or to a generic page for more serious, application crashing errors). This leaves you prone to a redirection chain of errors though (recursive), that will time out the requests eventually leaving the user or the developer with no clear error message at all. Always redirecting on errors, especially the application crashing errors, seems to be a bad idea because of this.
I noticed using ob_clean() WITHOUT ob_start() works on my project, deleting the echoed content already on a page. But the php manual states it should not work without ob_start(), and it should not even clear the echoed HTML, merely what was on the buffer. What is going on here?
-
Rather than ob_start, you could save all your print/echo statements in an $array = array(). The performance hit is lesser as far as I understand, but that can make the code ugly to deal with.
Appreciate the help