change php error output to custom message with link
How do I change the error output of a php error? For example if its a syntax error, or a server side time out, i want to echo a message that allows the user to refresh the page.
Heres the code I want开发者_如何学编程 to add my error message to:
$XML->registerXPathNamespace('tree','www.tree.com'); <--occasionally errors here, so I want to output my own error message.
Use the function set_error_handler
to define a custom function to be called when there is an error. You can then decide to do whatever you want within that function with the error.
If you only want it for a specific duration, you can restore it afterwards with restore_error_handler
.
set_error_handler('yourHandler');
...
$XML->registerXPathNamespace('tree','www.tree.com');
...
restore_error_handler();
function yourHandler(int $errno , string $errstr) {
//show link to refresh page, whatever. full signature can be found on PHP manual page
}
Well, quick check in php manual for registerXPathNamespace shows that it returns TURE
or FALSE
. Which is perfect for my solution.
if (!@$XML->registerXPathNamespace('tree','www.tree.com')) {
echo '<b>ERROR:</b> Could not register xml path. Please reload the page!';
}
Not that, if it returns FALSE, then the message will be displayed. And the @
in front of $XML will disable the original errors that, that action may cause.
精彩评论