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 '&l开发者_开发百科t;/pre>';
}
How can I get my errors to print inside the tags of my page?
Or, is there a better method to do this?
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>
?>
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 ;)
精彩评论