When developing PHP applications, error logs are underused because of their perceived complexity. PHP error logs are helpful, especially when configured and used correctly.
While there are more advanced techniques for maximising the potential of error logs, this guide will cover the basics of logs in PHP, their configuration, and where you can find them so you can get up and running quickly.
Table of contents
- Types of PHP errors
- Enable error logging
- View error logging
1. Types of PHP errors
PHP errors occur when something is off-base within the code. They can be as complicated as calling an incorrect variable or as simple as missing a semicolon. Understanding the types of errors is essential before solving them effectively.
• Warning error
PHP warning errors alert about a problem that may cause a more critical error in the long run. PHP will continue to execute the script despite encountering a warning, but it signifies a potential issue that could lead to unexpected behaviour. Common warning scenarios include using undefined variables or attempting to include a missing file.
• Notice error
Notices are the mildest type of error in PHP. They are informative messages about potential issues in your code but do not halt script execution. Notices typically occur when working with uninitialised variables or using deprecated functions.
• Syntax error
Syntax errors are the most common PHP error. They occur when your code violates the rules of the PHP language. These errors prevent the script from running and must be fixed before your code can execute. Examples include missing semicolons, unmatched parentheses, or typos in function or variable names.
• Fatal error
Fatal errors are severe issues that typically result in script termination. These errors can stem from issues like trying to call a non-existent function or include a file that does not exist. Fatal errors halt the script’s execution and display an error message. They demand immediate attention and debugging.
Fatal errors fall into three categories:
– When the framework cannot run the code because of an error in the installation process, a fatal error is reported during startup.
– A fatal error is generated at compile time when a developer uses a variable or data type that does not exist.
– A fatal error during program execution is called a runtime fatal error and is identical to the compile time fatal error.
2. Enable error logging
* You should always back up your website before making any changes to your website.
1) Locate the php.ini file on your server.
2) Use a text editor to open the php.ini file.
3) Look for lines containing the error_reporting, display_errors, and log_errors entries.
4) Set error_reporting to the level of errors you want to log. Common values include:
E_ALL – Logs all errors and warnings
E_ERROR – Logs fatal runtime errors
E_WARNING – Logs non-fatal runtime errors
E_NOTICE – Logs runtime notices (typical bugs in code)
5) Set log_errors to On to enable error logging.
6) Set error_log to the file path where you want to store the error log. For example:
error_log = /var/log/php_errors.log
* Ensure the specified directory and file have the necessary write permissions.
7) Save your changes to the php.ini file and close the text editor.
8) Restart your PHP processes.
3. View error logging
You can check the PHP error log only when it is enabled. In most cases, it is turned off by default. So, before changing the code and the configuration, check the PHP info to ensure that PHP logging is enabled on your PHP hosting account.
Method 1: Using the .htaccess file
You can enable the PHP error log by editing the .htaccess file. Locate the .htaccess file in the website’s root directory using the file manager or an FTP client. However, go to the control panel’s settings or FTP client to show hidden files if the file is hidden.
Once you have located the .htaccess file,
a) Access the .htaccess file and insert the following code:
php_flag log_errors on
php_value error_reporting 32767
php_value error_log “error_log.txt
b) Create a file titled error_log.txt in the public_html directory
c) Save and close the file
d) All PHP error logs will be reported in the error_log.txt file
If the website experiences trouble, open the error_log.txt, and you will see the PHP error logs.
Method 2: Using the php.ini file
One way to activate the PHP error log is by editing the php.ini file. It comes with a preconfigured web server that helps to run the PHP applications. The php.ini configuration file can be accessed through the PHP information. Use CTRL + F (Windows) or Command + F (Mac OS) to open the search box. Look for “Loaded Configuration File”.
You can also use the command line to locate the configuration file. Use an SSH client for connecting to the web server. Run the following command:
php -i | grep php.ini
In conclusion, setting up PHP error logging is crucial for any web developer or administrator. It provides valuable insights into your application’s health, helping you identify and resolve issues swiftly. By following the steps in this guide, you will be well-equipped to keep track of errors, enhance your website’s performance, and ensure a seamless user experience for your visitors.