In order to troubleshoot any issues with your PHP code, PHP errors can be enabled to display on your site or log to a file using your php.ini. This article will explain how to properly log PHP errors, display them on your site, and explain the ini_set() function for individual error checking on specific pages.
Logging PHP Errors
In most cases you can set the PHP error log or error reporting in the php.ini file. For most errors, it is best to use logging, as PHP errors can cause the entire site to stop working. By default, error reporting is disabled, and you would need to turn it on to get more information about what may be causing the problems you are having. The following guide will explain how to enable error_logging, as well as how to set the location of the log files.
Within the php.ini file, you can set the following line of code to On to log errors or off to turn error logging off.
log_errors = On
Using the php.ini, you can dictate where the error log will be located, based upon the location you provide.
This will place the error_log in the directory the error occurs in, as it is relative and not given a path.
; Log errors to specified file. error_log = error_log
This will place all PHP errors in the error_log file located in the directory you specify, as it is an absolute path to the file.
; Log errors to specified file. error_log = /path/to/site/error_log
If you are not sure where the php.ini file is located to make these changes, it is recommended that you contact your host.
Displaying PHP Errors
In the case you do not want errors to display site wide, and you only want to check errors on a single page, you can use the ini_set() function to have them displayed. The syntax for this is as follows::
string ini_set ( string $varname , string $newvalue )
This can be placed at the top of your PHP page with the error_reporting variable in it to allows error checking for that particular page.
- Navigate to the PHP file you want to check errors for.
- Add the following code to the top of the page.
ini_set('display_errors', '1'); 1 = On, 0 = Off
PHP has a list of different error reporting settings within the php.ini filea> itself. For example if you just want to display warnings only you can change the error_reporting to the following:
error_reporting = E_WARNING
To set multiple options, you can follow the format outlined here:
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
The following is a list of all available options for php.ini:
- E_ALL: All errors and warnings
- E_ERROR: fatal run-time errors
- E_WARNING: run-time, non fatal warnings
- E_PARSE: compile-time parse errors
- E_DEPRECATED: notices for the use of functions that will not be present in a future version
- E_NOTICE: run-time notices (these are warnings which often result from a bug in your code, but it’s possible that it was intentional
- 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
- E_COMPILE_WARNING: compile-time warnings (non-fatal errors)
- E_USER_ERROR: user-generated error message
- E_USER_WARNING: user-generated warning message
- E_USER_NOTICE: user-generated notice message
Congratulations! Once you have error reporting set to log or display, you will be able to see more information about the issue at hand. For further information regarding troubleshooting PHP issues, it is recommended that you review our PHP Troubleshooting for BoldGrid Guide.
Get more for free by signing up with BoldGrid Central Create Account