Disable errors in PHP
How to disable errors just for particular php function, and in the same time to know that the error occurred? For instance开发者_JAVA技巧, I use a php function parse_url, to parse an array of urls, sometimes it returns an error, what I want to do is to parse next url once error occurs, and not to show it(error) on the screen.
You might want to take a look at set_error_handler()
.
You can do anything you want registering your own handler.
the @ symbol before a function will suppress the error message and parse_url() returns false on erroring so just catch that.
if(@parse_url($url) === false) {
//Error has been caught here
}
@ is evil.
You can use this:
try {
// your code
} catch (Exception $e) {
// a block that executed when the exception is raised
}
Sounds like you are looking for a custom error handler
http://php.net/manual/en/function.set-error-handler.php
the best way is to convert php "errors" to exceptions, using the technique outlined here http://php.net/manual/en/class.errorexception.php and then handle an Exception like you do in other languages:
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
try {
parse_url(...)
} catch(Exception $e) {
Prefixing your function call with an @
, so using @parse_url()
should hide the error
To silent errors for a particular statement, simply add a @
.
Example
$file = @file_get_contents($url);
When file_get_contents
can throw error when $url is not found.
You can use the @ sign anywhere to silent any statements. Like:
$i = @(5/0);
精彩评论