Ray Alex
Ray Alex

Reputation: 475

Handling and displaying PHP errors at bottom of page

I'm using PHP's set_error_handler(brodeur) so that I can customize the behavior of error handling.

Now I find myself wanting to customize the display location/positioning of the output. Currently, as expected, the errors are displayed where the offending PHP code is located within the markup.

I would preferably like to have the output displayed at the very bottom or very top of the page so that it does not disturb the CSS/layout of the page content.

My issue is that I declare the brodeur function first in my code, then define the set_error_handler(brodeur) code, then comes the <HTML>...</HTML> content.

If I created a DIV wrapper at the bottom of my page (or top) what would be the best way to then place these error strings within that DIV, when the PHP code to do so will not be located within the DIV? Or are there any alternatives I am missing out on?

Upvotes: 3

Views: 560

Answers (1)

skrilled
skrilled

Reputation: 5371

There are multiple ways to go about this. Ideally, you are already separating your views/templates from your actual code. If so, you can set your errors as an array and pass it into the template, and then put it wherever you want.

If you aren't going about it in that method though, you want to use the try and catch functionality of php to handle the error:

$errors = array();
try {
 // do all your code here
}
catch(Exception $e)
{
  // do whatever you want with the error
  // my example is pushing it into an errors array..
  array_push($errors,$e->getMessage());
}
// now do whatever you want with $errors when you're ready..

Upvotes: 3

Related Questions