PHP Error Types and Error Logging Explained

April 4, 2024

PHP errors are common in web development. The errors range from minor syntax mistakes to more complex code issues. Knowing how to log and manage PHP errors is necessary when troubleshooting issues and improving code quality.

Configuring report settings and interpreting different error types is essential for efficient system management.

This guide explains PHP error logging, including how to display, configure, and interpret error logs.

PHP Error Logging Explained

Prerequisites

  • PHP installed (either as part of a web server or as a command-line tool).
  • Access to the command line/terminal.
  • Appropriate permissions to access web server configuration files.

How to Display Errors in PHP

For security reasons, PHP errors are not displayed by default. However, there are several ways to show PHP errors for development and testing purposes. Use the method that best suits your development environment.

Enable PHP Error Logging via php.ini File

The GUI or the command line can be used to enable PHP error logging via the php.ini file. In both cases, the display_errors directive controls whether PHP errors appear in the output.

Follow the steps below to enable PHP error logging via the GUI or command line.

GUI

To locate and edit the php.ini file via the GUI, do the following:

1. Find the php.ini file in the PHP installation directory. The location differs depending on the setup:

  • Apache: /etc/php/[version]/apache2/
  • CLI: /etc/php/[version]/cli/

Replace the [version] placeholder with the actual PHP version number.

2. Open the php.ini file with a text editor, like gedit. For example, run the gedit command from the terminal with:

sudo gedit /etc/php/[version]/apache2/php.ini
sudo gedit php.ini terminal open

Editing the file requires elevated (sudo) privileges.

3. Locate and change the following directive:

display_errors = On
php.ini display_errors directive On

By default, the directive is set to Off.

4. Save the changes and close the php.ini file.

5. Restart the web server for the changes to take effect. To restart Apache, run:

sudo systemctl restart apache2

The restart automatically applies the changes. PHP errors are now visible in the CLI or the browser, depending on the setup.

Command Line

To enable error logging in the php.ini file via the command line, do the following:

1. Navigate to the PHP installation directory using the cd command. The location differs depending on how PHP was installed:

  • Apache:
cd /etc/php/[version]/apache2/
  • CLI:
cd /etc/php/[version]/cli/

Replace [version] with the installed PHP version number.

2. Open the file using a text editor. For example, to open the file with nano, run:

sudo nano php.ini

Editing the file requires sudo privileges.

3. Adjust the following directives in the file:

display_errors = On
nano php.ini display_errors  = On

The directive is set to Off by default.

4. Save and close the text editor.

5. Restart the web server to apply the changes:

sudo systemctl restart apache

Adjust the command if using a different server.

Enable PHP Error Logging via .htaccess

Enabling PHP error logging via the .htaccess file allows errors to be logged per directory. This method does not require changing the default server configuration.

Note: If using WordPress, follow our guide to create and edit the WordPress .htaccess file.

To enable PHP error logging via .htaccess, do the following:

1. Locate or create the .htaccess file. It should be in the same directory as the PHP file that requires error logging.

2. Open the .htaccess file with a text editor.

3. Add the following logging directives:

php_flag log_errors On
php_value error_log [path]/php_errors.log

Replace the placeholder [path] with the actual path to the error log file.

4. Save the changes and close the .htaccess file.

5. Ensure the web server has appropriate access permissions for writing and editing the log file. Use the chmod command to change the file permissions, for example:

sudo chmod 644 php_errors.log

These permissions give the file owner read and write access, while all other users have read-only access. The directory where the log file resides also requires the same permissions.

PHP Error Types and Their Meanings

Knowing different PHP error types and their meanings is essential for troubleshooting and debugging code. The sections below explore PHP errors and their differences.

PHP Parse (Syntax) Error

Syntax errors cause parse errors in PHP. This error type appears when there are:

  • Extra/missing brackets, quotes, semicolons.
  • Misspelled keywords, variables, and function names.

For example, the following script deliberately signals a parse error:

<?php
echo "Hello, world!
?>
PHP parse error terminal output

The script generates a parse error due to a missing closed quote.

PHP Notice Error

Notice errors are non-critical errors. These errors happen due to:

  • Using undefined variables/constants.
  • Accessing non-existing array indexes.

For example:

<?php
echo $a;
?>
PHP notice error terminal output

The example attempts to access an undefined variable $a. The action triggers a notice error and continues script execution.

PHP Warning Error

Warning errors in PHP are more severe than notices. They show a problem in the code that is not severe enough to stop script execution. Causes for warning errors include:

  • Using deprecated functions.
  • Including external files that do not exist.
  • Passing wrong function parameters.
  • Incorrect SQL queries or database connections.

For instance:

<?php
include ("external_file.php");
?>
PHP warning error terminal output

Since external_file.php is not in the current directory, the output displays a message notifying there is no such file.

PHP Fatal Error

Fatal errors stop program execution. The main reason for this error type is an undefined function or class in the script. There are three types of fatal errors:

  • Startup fatal errors occur when the system cannot run the code during program startup and installation.
  • Compile time fatal errors happen when you try to use non-existent data.
  • Runtime fatal errors appear while the program runs, causing the code to stop execution.

For instance, the following script results in a runtime fatal error:

<?php
function divide()
{
  $result=6/0;
  echo "The result: ".$result;
}
divide();
?>
PHP fatal error terminal output

The example attempts to divide a number by zero. It results in a fatal error and stops script execution.

PHP Error Messages: Comparison

PHP error types have different urgency and show distinct output messages. The table below provides a brief comparison between different PHP error messages and their meaning:

TypeSeverityMeaningEffect
Parse errorHighSyntax error.Halts script execution.
Notice errorLowNon-critical code issue.Does not halt script execution.
Warning errorMediumPossibly a problematic code issue.Possibly a problematic code issue.
Fatal errorCriticalUrgent code issue.Halts script execution.

Additional php.ini Error & Logging Configurations

There are many additional php.ini error and logging configurations. The following directives are helpful to configure PHP error logging further:

  • error_log. Allows specifying a custom file location for error logs. Use this directive to change the default error log location. For example:
error_log [path]/php_errors.log
  • error_reporting. Defines the error log reporting level. The directive uses predefined constants to define the level. For example, to report all errors except notices, use the following format:
error_reporting E_ALL & ~E_NOTICE
  • log_errors_max_length. Sets the error log maximum length. Longer messages truncate, improving readability and shortening log length. For example, to limit error message length to 100, use:
log_errors_max_len = 100
  • html_errors. Restructures error messages into HTML code. The directive is useful if error messages appear in the browser instead of a log file. Enable it with:
html_errors = On

Note: Exposing internal server error messages is a cybersecurity risk and creates a cross-site scripting (XSS) vulnerability. Read more about website security and implement best practices.

Additional PHP Error & Logging Functions

PHP features additional error and logging functions besides the core error-handling mechanisms. These functions perform custom tasks and enable flexible error logging.

Noteworthy PHP error and logging functions are:

  • error_log(). Enables logging error messages and logs to various destinations, including error logs, files, or custom error handlers. It allows developers to record errors and create custom error-handling mechanisms in PHP. For example:
$message = "Custom error message";
error_log($message);
  • error_reporting(). Gets or sets the error reporting level at runtime. For example:
// Get current reporting level
$currentLevel = error_reporting()

// Set reporting level
error_reporting(E_ALL)
  • ini_set(). Sets PHP settings at runtime, including logging and error handling configuration. For example, to set the error reporting level using this function, use:
ini_set('error_reporting', E_ALL);
  • trigger_error(). Generates a user-level custom error, warning, or notice message. Use this function to pass a message to a custom error handler. For example:
$message = "Custom error message";
trigger_error($message, E_USER_ERROR);

PHP Error & Logging Constants

PHP functions and configurations use various constants. The constants help define reporting levels, error types, and logging options. The sections below list commonly used PHP constants for error logging.

Reporting Levels

Reporting level constants define the scope of error type in reports. The table below lists reporting level constants:

ConstantDescription
E_ERRORFatal errors.
E_WARNINGRuntime warnings.
E_NOTICERuntime notice errors.
E_PARSEParse errors.
E_ALLAll error types.

Use these constants in PHP to set the reporting level with the error_reporting() function.

Error Types

Error types are constants for custom errors. The following table lists different custom error types with a short description:

ConstantDescription
E_USER_ERRORUser-generated fatal errors.
E_USER_WARNINGUser-generated warnings.
E_USER_NOTICEUser-generated notice errors.
E_RECOVERABLE_ERRORFatal error that can be caught.
E_DEPRECATEDDeprecated feature error.
E_USER_DEPRECATEDUser-generated deprecated feature error.

When defining a custom error handler with the trigger_error() function, use the error type constants.

Logging Options

Logging option constants help specify log message severity. See the table below for different log levels:

ConstantDescription
LOG_EMERGVery severe conditions that require immediate attention.
LOG_ALERTSevere conditions that require immediate action.
LOG_CRITCritical errors that require attention.
LOG_ERRError conditions that do not require immediate action.
LOG_WARNINGNon-critical warnings that do not stop application functions.
LOG_NOTICEImportant informational messages about the system or application.
LOG_INFOInformational messages about the system or application.
LOG_DEBUGDebugging messages with detailed diagnostics.

Use these constants in the error_log() function to categorize logs based on severity.

Conclusion

After reading this guide, you know how PHP error logging works, the different error types, and various available configurations. Research additional constants, functions, and configurations to customize the error-logging mechanisms.

Next, check out our PHP error reporting guide.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Connect to MySQL Using PHP
April 23, 2024

To access and add content to a MySQL database, you must first establish a connection between the database and ...
Read more
How To Install PHP 7, 7.2 & 7.3 On CentOS 7
May 24, 2019

PHP is a programming language that's often used to automate server tasks. It's part of the LAMP...
Read more
How To Install PHP On Ubuntu 20.04 or 22.04
November 24, 2022

PHP is a script-based server-side programming language. PHP is often used to automate server tasks and is the ...
Read more
PHP Error Reporting: How to Enable & Display All Errors / Warnings
April 4, 2024

If a script is not written correctly, or if something unusual happens, PHP can generate an error message. The...
Read more