On this quick tip on PHP error reporting, we’ll take a look at the right way to use the tools available in PHP to handle errors in a controlled way and thereby save hours of debugging.
PHP is, by definition, an “exception-light” programming language. Which means that, while it does have exceptions, it’ll proceed to execute any script no matter what happens unless a fatal error occurs.
For instance:
echo $sitepoint;
The code above will return the next message:
Notice: Undefined variable: sitepoint in PHP shell code on line 1
PHP will only throw a notice error, and can happily proceed executing. An “exception-heavy” language like Python will throw an error and halt execution.
For this reason behavior, PHP developers should be extra careful when writing their code. Unexpected leads to the execution of programs might occur, because notices won’t halt the execution but may impact the proper behavior of this system.
Before we go into the right way to adjust the error reporting style in PHP, let’s first understand the several levels of PHP error severity.
PHP has three primary kinds of messages: errors, notices, and warnings. These represent different levels of severity: E_ERROR, E_NOTICE, and E_WARNING.
-
Errors are fatal runtime errors and are often attributable to faults within the code. This may cause PHP to stop executing.
-
Notices are messages attributable to code which will or may not cause problems (for instance, an undefined variable). These is not going to cause an execution halt.
-
Warnings are non-fatal errors and the script execution won’t be halted.
Error Logging in PHP
By default, PHP doesn’t log any errors. For that to occur, we now have to specifically tell it to begin logging by turning on the display_errors variable on the PHP configuration file (the php.ini file).
On this file, we are able to moreover tell PHP if we also need to log notices and warnings, and where this log needs to be recorded.
There’s also the likelihood to trigger logging from throughout the code. To do that, we are able to use the error_log() function. Since error logging isn’t the primary focus of this text, more information could be found here.
Changing PHP Error Reporting
We are able to change the default PHP error reporting behavior by utilizing the error_reporting() function. With this function, we are able to set the extent of errors during the script. This is finished by passing a number of of the predefined error constants to the function.
For instance, if we wish to see not only errors but additionally notices we could use this:
error_Reporting(E_ERROR | E_NOTICE);
With this declaration, the script execution will likely be halted not just for errors but additionally for notices.
Suppressing Errors
We may tell PHP to suppress specific errors by utilizing the error control operator (@). By putting this operator in the beginning of an expression, any error that’s a direct results of that expression is silenced:
echo @$sitepoint;
This may output the worth of $sitepoint if it exists, but it surely’s going to return a NULL and print nothing (as a substitute of throwing a notice) if it doesn’t.
Be very careful when using this operator, as it’ll completely hide the error. Not only will the error not be displayed, but it surely also won’t be sent to the error log.
While it could seem harmless, by utilizing this operator you’ll be masking deeper structural issues inside your code and covering up potential errant behaviors.
PHP as an Exception-heavy Language
Finally, PHP may also be used as an “exception-heavy” programming language. Normal PHP errors could also be thrown as exceptions by utilizing the ErrorException class that extends the PHP Exception class.
In the next example, a user-defined function called errorhandler() is ready as error handler with the set_error_handler() function. It throws an ErrorException when a fatal error occurs because a file isn’t found with the file_get_contents() function:
function errorHandler($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) {
return;
}
throw recent ErrorException(“Fatal Error:No such file or directory”, 0, E_ERROR);
}
set_error_handler(“errorHandler”);
try {
$data=file_get_contents(“sitepoint.txt”);
echo $data;
} catch (ErrorException $e) {
echo $e->getMessage();
}
By utilizing this method, we are able to handle execution errors the way in which we handle exceptions, wrapping them in a try…catch statement with proper instructions on the right way to behave in such situations.
Conclusion
Summing up, PHP may handle errors in a really loose fashion. It’s as much as us developers to make use of the available tools to higher handle them so we are able to make probably the most out of the language. By utilizing this set of tools, we are able to handle errors in a controlled way, saving hours of debugging this manner.