During the execution of a PHP script, many types of errors and warnings are generated. Errors and error reporting in PHP are something that many newcomers to the language may overlook at first. It is because PHP errors get muted by default on many PHP-based web server configurations.
Activating error messages in the production phase is not a good idea because they will be displayed to the users, but it is critical to allow error messages during the development phase (staging phase) so that we are aware of any difficulties beforehand. As a result, knowing how to enable them, especially for your local development environment, is a smart idea. This makes it easier to spot mistakes in your code for making flawless projects.
What is a PHP Error?
When there is a problem with the PHP code, a PHP error occurs. Even anything as basic as improper syntax or omitting a semicolon might result in an error, prompting a warning. Alternatively, the reason might be more complicated, such as invoking an incorrect variable, which can result in a fatal error and cause your system to crash. In PHP, there are primarily four types of errors to consider:
- Syntax Error or Parse Error
- Fatal Error
- Warning Error
- Notice Error
How to Display all PHP Errors
An individual has to manually activate error reporting if they’re not getting errors. Edit your PHP code file and add the following lines to enable error reporting:
You may also allow error reporting using the ini set command:
What Do These Code Lines Do?
The ini set function will override the configuration of PHP ini file. The display errors and display startup errors commands are just two of the options. The display errors command determines whether or not the errors should be displayed to the user. After development, the display errors directive should typically be disabled.
The display_startup_errors directive, on the other hand, is a distinct directive since display errors do not handle problems that occur during PHP’s startup process. Unfortunately, parsing issues such as missing semicolons or curly brackets will not be shown by these two directives. The php.ini settings must be changed in this situation.
Enabling PHP Error Reporting in php.ini
If your PHP code is set to display errors but they still aren’t showing up, you may need to make modifications to your php.ini file. The file is generally found in the /etc/php.ini folder on Linux distros. To get started, open php.ini in a text editor. Then turn on the display_errors line.
display_errors = on
Doing this configuration will show all the problems, including syntax and parsing errors, that can’t be seen by just invoking the ini set function in PHP code as done in the above section. The PHP ini file is labeled as a loaded configuration file and may be located in the reported output of the phpinfo() function. If the application is in production, these configurations in the ini configuration must be set to off.
Turn on Error Reporting by Editing the .htaccess File
The main configuration file, .htaccess, is generally stored in the base or public directory. The dot at the start denotes that it is concealed. You’ll need to change the settings in your file manager to see this hidden file. Right after opening the file, add the following code to it.
php_flag display_startup_errors on
php_flag display_errors on
.htaccess includes directives for display startup errors and display errors, similar to what would be added to the PHP code to show PHP problems. The benefit of displaying or silencing error messages in this way is that development and production may have separate .htaccess files, with production suppressing error displays.
You may want to modify display errors in .htaccess or your PHP.ini file depending on which files you have access to and how you perform deployments and server configurations. Many hosting companies won’t let you change the PHP.ini file to enable display errors.
Programmers can also enable a custom error log in the .htaccess file, as long as the log folder or log file is readable by the web server. The log file can be either a relative path to the .htaccess file or an absolute path, such as:
php_value error_log logs/all_errors.log
Error_Reporting() Function in Detail
The error reporting function is a PHP function that allows developers to select which and how many problems are shown in their applications. Remember that this method will set the error reporting directive in the php.ini settings during runtime.
The argument zero should be provided to the error reporting method to eliminate all errors, warnings, parse messages, and notifications. This piece of code would be impractical to include in each of the PHP files. Therefore, it is preferable to disable report messages in the php.ini file or the .htaccess file.
Undeclared variables will be shown in the web application if the aforementioned command is provided in the error reporting method. However, this isn’t recommended since undeclared variables might cause problems in the program when they’re utilized in loops and conditions. This can also cause problems when the stated variable is spelled differently from the variable used in conditions or loops.
error_reporting(E_ALL & ~E_NOTICE);
You may filter which mistakes are displayed using the error reporting function. The “~” character stands for “not” or “no”. Therefore, the parameter E_NOTICE indicates that notifications should not be displayed. Between the potential options, note the “&” and “|” letters. The “&” symbol stands for “true for all,” whereas the “|” character stands for “true for either”.
The above three lines of code have the same effect: they display all PHP problems. Because it is more clear and intelligible, the error reporting(E_ALL) method is the most popular amongst developers for displaying error messages.
Log PHP Errors to a File
Error messages must not be displayed to end-users throughout the operation, but they must be logged for tracing reasons. The easiest method to keep track of these error messages is to keep track of them in log files.
The error log function, which accepts four arguments, provides a simple way to use log files. The first argument, which provides information about the problem or what should be logged, is the only one that must be provided. This function’s type, destination, and header arguments are all optional.
error_log(“There is something wrong!”, 0);
If the type option is not specified, it defaults to 0, which implies that this log information will be added to the web server’s log file.
error_log(“Email this error to someone!”, 1, “firstname.lastname@example.org”);
The type 1 parameter will email an error log specified in the third parameter. To use this functionality, php.ini must have a valid SMTP setup to send emails. Host, encryption method, username, password, and port are among the smtp.ini instructions. This kind of error reporting is recommended for logging or informing errors that must be corrected as soon as possible.
error_log(“Write this error down to a file!”, 3, “logs/my-errors.log”);
Type 3 should be for logging messages in a different file established by the web server’s settings. The third argument specifies the log file’s location, which must be readable by the web server. The log file’s location might be either a relative or absolute path to where this code is invoked.
How to Stop Error Reporting in PHP
Set the value to 0 to turn off or deactivate PHP error reporting. Use the following code snippet as an example:
Logging PHP Errors via Web Server Configuration
The easiest approach to record errors is to define them in the web server configuration file, rather than altering parameters in the .htaccess or adding lines in the PHP code to display errors.
These files should be introduced to the virtual host of a particular website or app in Apache, which is generally found in the sites-available folder in Ubuntu or the httpd-vhosts file in Windows.
The directive is just named error log in Nginx, as it is in Apache. The log files for both Apache and Nginx web servers must be readable by the web server. Fortunately, the directories for the log files of these two web servers are already readable after installation.
So far, the blog has covered different ways to enable and display all PHP errors and notifications. You may increase your ability to handle PHP difficulties by getting error messages fast and precisely. It’s critical to know which PHP version your web server is running before introducing new features, installing new PHP-based software, or trying to track down the errors on your applications or websites.