In this quick tip on PHP error reporting, we’ll look at how to use the tools available in PHP to handle errors in a controlled manner, saving hours of debugging.
PHP is by definition an exception-light programming language. This means that while there are exceptions, each script will continue to run no matter what happens unless a fatal error occurs.
For example:
<?php
echo $sitepoint;
The above code returns the following message:
Notice: Undefined variable: sitepoint in PHP shell code on line 1
PHP will just throw a notice error and continue execution without any problems. An “exception-heavy” language like Python throws an error and stops execution.
This behavior requires PHP developers to be extra careful when writing their code. Unexpected results can occur when running programs because hints do not stop execution, but can affect the correct behavior of the program.
Before we get into customizing the error reporting style in PHP, let’s first look at the different severity levels of PHP errors.
PHP has three main types of messages: Mistake, HintsAnd warnings. These represent the different degrees of severity: E_ERROR
, E_NOTICE
And E_WARNING
.
-
Mistake are serious runtime errors and are usually caused by errors in the code. This causes PHP to stop running.
-
Hints are messages caused by code that may cause problems (e.g. an undefined variable). These do not lead to an execution stop.
-
warnings are non-fatal errors and script execution is not halted.
Error logging in PHP
By default, PHP does not log errors. For this to happen we need to explicitly tell it to start logging by turning on that display_errors
Variable in the PHP configuration file (the php.ini
File).
In this file we can also tell PHP whether we also want to log notes and warnings and where this log should be recorded.
It is also possible to trigger logging from the code. We can use the error_log()
Function. Since error logging is not the focus of this article, more information can be found here.
Change PHP error reporting
We can change the default behavior of PHP error reporting using: error_reporting()
Function. This function allows us to set the error level for the duration of the script. This is done by passing one or more of the predefined error constants to the function.
For example, if we want to see not only errors but also hints, we could use:
<?php
error_Reporting(E_ERROR | E_NOTICE);
With this declaration, the script execution is not only stopped on errors, but also on hints.
suppress errors
We can also tell PHP to suppress certain errors by using the error control operator (@
). Inserting this operator at the beginning of an expression suppresses any error that is a direct result of that expression:
<?php
echo @$sitepoint;
This returns the value of $sitepoint
if it exists, but it will return a NULL
and don’t print anything (instead of throwing a hint) if it doesn’t.
Be very careful when using this operator as it completely hides the error. Not only will the error not displayed, but also not sent to the error log.
Although it may seem harmless, by using this operator you mask deeper structural problems in your code and cover up potentially erroneous behavior.
PHP as an exception-heavy language
Finally, PHP can also be used as an “exception-heavy” programming language. Normal PHP errors can be thrown by using as exceptions ErrorException
Class that extends PHP Exception
Class.
The following example calls a user-defined function errorhandler()
is set as an error handler with the set_error_handler()
Function. It throws one ErrorException
when a fatal error occurs because a file cannot be found with the file_get_contents()
Function:
<?php
function errorHandler($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) {
return;
}
throw new 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 using this method, we can handle execution errors in the same way as exceptions, by enclosing them in a try…catch
Statement with appropriate instructions on how to behave in such situations.
Diploma
In summary, PHP may be very lax about errors. It’s up to us developers to use the tools available to better handle them so we can get the most out of the language. By using these tools, we can handle errors in a controlled manner, saving hours of debugging.
0 Comments