For web developers and IT professionals, encountering PHP errors is an inevitable part of the development process. Understanding how to identify, troubleshoot, and resolve these errors efficiently can significantly improve your workflow and code quality. This guide will provide you with an overview of common PHP errors.
Show Me the Error
The first step to dealing with PHP errors is to see the error. There’s a few different ways to do this, if it’s a development or testing environment then you will probably want to see the error when you load the page without having to go checking through logs. Either way, you will want to enable error reporting to log the errors. You can do this through the PHP config file (either php.ini or user.ini files) or you can simply do it in the PHP code with the following code:
ini_set('display_errors', true);
error_reporting(E_ALL); //This will display all kinds of errors, from warnings, to deprecation, to notices
E_ALL will show you every single kind of issue. From an undefined variable to fatal errors and everything in between. If you’re on a production or live server then you will probably want to report the errors to a log file and not show them to every person who comes to the site. For that we will not show the errors, but report them all in a file called errors.txt.
ini_set("error_log", "errors.txt");
ini_set('display_errors', true);
error_reporting(E_ALL); //This will display all kinds of errors, from warnings, to deprecation, to notices
Log files don’t have to end in txt, in fact you can have error log files on Linux and Windows servers to just be something without an extension, like “errors”. Personally, what I like to do is only show the errors to me, but not my users. I brought this up in my tutorial for dealing with WordPress errors, but the concept is the same. I have it check my IP address and if it matches, then I will show the error on the page, if it doesn’t match then the user won’t see any error at all. To do this, we use an if statement.
if($_SERVER['REMOTE_ADDR']=="YOUR IP ADDRESS"){
ini_set('display_errors', true);
error_reporting(E_ALL); //This will display all kinds of errors, from warnings, to deprecation, to notices
}else{
ini_set("error_log", "errors.txt");
ini_set('display_errors', true);
error_reporting(E_ALL); //This will display all kinds of errors, from warnings, to deprecation, to notices
}
Understanding PHP Error Messages
The above code will tell you a lot about your code. It will tell you every single error that will prevent the script from finishing, but it will also tell you every warning and notice that something will be changed in a future version of PHP. What if we don’t want to see all undefined indexes and undefined variables though? What about if you only want to see errors that will prevent the code from completing? To do that, instead of using the E_ALL constant, you would use the E_ERROR constant. So it would be:
error_reporting(E_ERROR);
What if we wanted errors and warnings though? You could do that using a single a bitwise OR, also known as a |:
error_reporting(E_ERROR | E_WARNING);
What if you want to do all errors except warnings? We can do that by excluding the warnings:
error_reporting(E_ALL & ~E_WARNING);
While warnings, deprecation errors, and notices will not necessarily prevent your code from completing, it is important to pay attention to them and get the issue fixed as it may end up being a fatal error in the next version of PHP. Listed below are all the error constants and a description of what they identify.
Constant | Description |
---|---|
E_ERROR |
Fatal runtime errors. These are errors that cannot be recovered from. Execution of the script is halted. |
E_WARNING |
Runtime warnings (non-fatal errors). Execution of the script is not halted. |
E_PARSE |
Compile-time parse errors. These should only be generated by the parser. |
E_NOTICE |
Runtime notices. Indicate that the script encountered something that could be an error, but could also happen in the normal course of running a script. |
E_CORE_ERROR |
Fatal errors that occur during PHP’s initial startup. |
E_CORE_WARNING |
Warnings (non-fatal errors) that occur during PHP’s initial startup. |
E_COMPILE_ERROR |
Fatal compile-time errors. These are generated by the Zend Scripting Engine. |
E_COMPILE_WARNING |
Compile-time warnings (non-fatal errors). These are generated by the Zend Scripting Engine. |
E_USER_ERROR |
User-generated error message. This is like an E_ERROR , but generated in PHP code using trigger_error() . |
E_USER_WARNING |
User-generated warning message. This is like an E_WARNING , but generated in PHP code using trigger_error() . |
E_USER_NOTICE |
User-generated notice message. This is like an E_NOTICE , but generated in PHP code using trigger_error() . |
E_STRICT |
Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility. |
E_RECOVERABLE_ERROR |
Catchable fatal error. It indicates that a dangerous error occurred, but did not leave the Engine in an unstable state. |
E_DEPRECATED |
Runtime notices. Enable this to receive warnings about code that will not work in future versions of PHP. |
E_USER_DEPRECATED |
User-generated warning message. This is like an E_DEPRECATED , but generated in PHP code using trigger_error() . |
E_ALL |
All errors and warnings, except of E_STRICT prior to PHP 5.4.0. |
Every developer will run into errors, but being able to see them and resolve them is one of the most important parts of coding. Hopefully the above helps you to solve your issues and get your code running smoothly.