3

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?

6
  • IMO, this is a non-issue. You should be handling errors from the error log. Displaying errors via UI is a bad practice. Commented Feb 22, 2013 at 22:54
  • @MikePurcell I disagree as long as the website is not running in production Commented Feb 22, 2013 at 22:55
  • @ExplosionPills: Ok, I will concede, if in a dev env outputting errors is a good thing so you can immediately see if there are any issues. For this I always use xdebug and let it throw the errors where they occur. Commented Feb 22, 2013 at 22:58
  • No, this would never be in production Commented Feb 22, 2013 at 23:03
  • 1
    @RayAlex: If you are only speaking about this situation within the context of a dev environment, the look into adding the xdebug module: xdebug.org Commented Feb 22, 2013 at 23:05

1 Answer 1

3

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..
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for template/code separation. If you go with try/catch, be aware that PHP is finicky about which errors can actually be caught.
Bah, I completely overlooked storing it (my mind bent on immediately displaying). Will accept momentarily - thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.