views:

44

answers:

2

I am wanting to throw custom errors in many places in some interfaces that I am building. Just using an echo inside my function places that echoed code above the tag in my page. So, I built a custom function to handle errors and give me a backtrace, but again, it prints above the in my page.

Here is the function I am using:

function error($msg) {
    trigger_error($msg, E_USER_WARNING);
    echo '<pre>';
    debug_print_backtrace();
    echo '</pre>';
}

How can I get my errors to print inside the tags of my page?

Or, is there a better method to do this?

+1  A: 

If your server runs php5, you shall use the exceptions handler.
It's exactly what you need and once you get how it works, they're really useful.

Here's a nice tutorial on how to use them ;)

Daan
+1  A: 

How would I delay printing errors? Ideally, I would love to have a Errors here that would always be put in the body if there is an error

I'm not sure if I completely follow what you are doing, but maybe this is it?

<?php
class ErrorHandler
{
  static private $errors = null;

  static function report( ... /* see set_error_handler() for params */ )
  {       
    self::$errors .= "...";
    return true;
  }

  static function getErrors()
  {
     return self::$errors;
  }
}

set_error_handler(array('ErrorHandler', 'report'));

// ...

echo "<div>".ErrorHandler::getErrors()."</div>
?>
konforce
The main changes I'd make is to turn `ErrorHandler::$errors` into an array and combine the items using `implode`, which more readily allows for additional processing of error messages. Also, consider using a list element rather than a div.
outis