Quickly Show All PHP Errors
The fastest way to display all php errors and warnings is to add these lines to your PHP code file:
What these lines do?
The ini_set function will try to override the configuration found in your PHP ini file. It accept two arguments, the varname and value.
The display_errors and display_startup_errors are the two varname examples of arguments. The display_errors argument will determine if the errors will be displayed or hidden to the user. Setting it to 1 means display otherwise if set to 0 then hide errors.
The display_startup_errors, however, is a separate directive because the display_errors doesn’t handle the errors that will be encountered during PHP’s startup sequence.
Those three lines are the most useful to me when I am debugging a php code. I usually put it as the first three lines in my index.php to reveal whatever errors might occurs and remove later when I commit to git.
However, these two arguments won’t be able to display parse errors such as missing semicolons or missing curly braces. In this case, the PHP ini configuration must be modified.
Display PHP Errors via .htaccess
Developers usually have access to the directory files. The varname for showing PHP errors can also be enabled or disabled using the .htaccess file located in the root or public directory of the project.
php_flag display_startup_errors on
php_flag display_errors on
The advantage of showing or disabling error messages in this manner is that development and production can have different .htaccess files, where the production suppresses the displaying of errors.
Many hosting providers will not allow you to modify your PHP.ini file to enable display_errors. Depending on which files you have access to and how you do deployments and server configurations, you may want to configure display_errors in .htaccess or your PHP.ini file.
A custom error log can also be enabled as long as the log folder or the log file is writable by the web server. The log file can be a relative path to where the .htaccess is located, or it can be an absolute path such as /var/www/website_name/public/logs.
Detailed Warnings and Notices
Sometimes, the warnings that seem not to affect the application at the start will cause some fatal errors in certain conditions. These warnings must be fixed because this means that the application won’t run normally under certain scenarios. In case these warnings cause a lot of errors, then it would be more practical to hide the errors and just show what the warning messages are.
To show warnings and notices, the parameter for the error reporting function will be “E_WARNING | E_NOTICE”.
The error_reporting function can accept E_ERROR, E_WARNING, E_PARSE, and E_NOTICE parameters. To report all errors except for notices, then the parameter is “E_ALL & ~E_NOTICE” where E_ALL stands for all the possible parameters of the error_reporting function.
The error reporting function is a built-in PHP function that allows developers to control which and how many errors will be shown in the application. Remember, the PHP ini configuration has an error_reporting directive that will be set by this function during runtime.
To remove all errors, warnings, parse messages, and notices, the parameter that should be passed to the error_reporting function is zero. It would be not practical to have this line of code in each of the PHP files. It would be better to turn off report messages in the PHP ini file or in the .htaccess.
PHP allows variables to be used even when not declared. This is not a standard practice because undeclared variables will cause issues for the application once it is used in loops and conditions. Sometimes, this also happens because the declared variable has a different spelling than the variable being used for conditions or loops. When E_NOTICE is passed in the error_reporting function, then these undeclared variables will be displayed in the web application.
error_reporting(E_ALL & ~E_NOTICE);
The error reporting function allows you to filter which errors can be shown. The “~” character means “not” or “no” so the parameter ~E_NOTICE means not to show notices. Notice the “&” and “|” characters in between the possible parameters. The “&” character is for “true for all”, while the “|” character represents either one as long as it is true. These two characters have the same meaning in PHP conditions OR and AND.
These three lines of codes do exactly the same thing, it will show all PHP errors. The error_reporting(E_ALL) is the most widely used among developers to show error messages because it is more readable and understandable.
Using the error_log() function to log on file
During production, error messages must not be shown to the end users, but this information must still be recorded for tracing purposes. The best way to record these error messages on a production web application is to store it in log files..
One easy way to use log files is to use the error_log function, which accepts four parameters. The only required parameter is the first parameter, which contains the details about the error or what’s to be logged. The type, destination, and header are all optional parameters for this function.
error_log("There is something wrong!", 0);
The type parameter, if not defined, will be 0 by default, which means this log information will be appended to whatever log file is defined in the web server.
error_log("Email this error to someone!", 1, "email@example.com");
The type 1 parameter will email an error log specified in the third parameter. To make this feature work, the PHP ini must have a proper SMTP configuration to be able to send out emails. These SMTP ini directives include host, encryption type, username, password, and port. This kind of error reporting is advisable to logging or notifying errors that need to be fixed as soon as found.
error_log("Write this error down to a file!", 3, "logs/my-errors.log");
To log messages in a separate file defined by the configuration of the web server, then type 3 must be used. The third parameter will serve as the location of the log file and must be writable by the web server. The location of the log file can be a relative path to where this code is called or an absolute path.
Log PHP Errors Through the Web Server Configuration
To avoid changing parameters in the .htaccess or adding some lines in the PHP code to show errors, then the best way to log errors is defining it in the web server configuration file.
error_log("There is something wrong!", 0);
For Apache, these files must be added to the virtual host of the specific website or application, usually located inside the sites-available folder for Ubuntu or httpd-vhosts file in Windows..
Log PHP Errors on File
When your php code is in production errors do happen to keep track these without given hint to malicious attacker it's good to log errors on file.
What this lines do? The ini_set function modify the php.ini file temporarily until it finished the scripts execution. The parameter error_log tells php to log errors on error.log file.