Intercepting all errors in PHP

Sometimes you get annoying errors in PHP that refer to the server error log. If you do not have access to the log, because your pretentious web hosting company does not allow you to access them, you need to catch any errors before they end up in the error log. Therefore, there is the amazing feature, set_error_handler , that allows you to intercept errors halfway to hell and manage them yourself.

Here is an example of a simple error handling:

<?php 
	function simpleErrorHandler($errno, $errstr, $errfile, $errline)
	{
		if (!(error_reporting() & $errno)) 
		{
			// This error code is not included in error_reporting
			return;
		}
 
		switch ($errno) 
		{
			case E_USER_ERROR:
				echo "<b>User error:</b> [$errno] '$errstr' - Error line $errline in file $errfile\n";
				exit(1);
				break;
 
			case E_USER_WARNING:
				echo "<b>User warning:</b> [$errno] '$errstr'\n";
				break;
 
			case E_USER_NOTICE:
				echo "<b>User notice:</b> [$errno] $errstr\n";
				break;
 
			default:
				echo "<b>Unknown error:</b> [$errno] $errstr\n";
				break;
		}
 
		/* Don't execute PHP internal error handler */
		return true;
	}
 
	$errorHandler = set_error_handler("simpleErrorHandler");
?>