[PHP] How to catch warnings inside a script
Put the case you’re doing a FTP upload of a file. If the transmission fails for whatever problem, you’ll receive a warning but your app won’t stop, you’ll only be able to see that an error occurred. So you can define an error handler to catch those warnings:
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E__ERROR: echo "FATAL ERROR ".
"[line: $errline] $errstr in $errfile\n";
break;
case E_WARNING: echo "WARNING [line: $errline]".
$errstr." in $errfile\n";
break;
case E__NOTICE: echo "NOTICE [line: $errline] $errstr ".
in $errfile\n";
break;
default: echo "UNKNOWN ERROR: [line: $errline] $errstr in $errfile\n";
}
/* Don't execute PHP internal error handler */
return true;
}
set_error_handler("myErrorHandler");
$a = 0; $b = 2;
$c = $b/$a;
echo $c; // not outputted
This will raise a warning:
WARNING [line: 21] Division by zero in /var/www/temp/warning.php