Table of contents
1.
Introduction 
2.
Quickly Show All PHP Errors
3.
How to Configure PHP.ini to Display All Errors and warnings? 
4.
Use of .htaccess file to Display PHP Error
5.
error_reporting () Function
5.1.
Syntax of error_reporting () Function
6.
Using the error_log() Function to Send PHP Errors to a File 
6.1.
Set the error logging in the script
6.2.
Set the error logging in php.ini file 
7.
Modern Methods to Handle PHP Errors 
7.1.
Method 1 - Using ini_set() for More Controlling Errors
7.2.
Method 2 - Using Try-Catch to Handle Exceptions in PHP 8.x
8.
Example
8.1.
PHP
9.
Frequently Asked Questions
9.1.
What is the difference between error_reporting and display_error in PHP?
9.2.
Why you should turn off display_error() method in the production environment? 
9.3.
What is the role of php.ini in error display?
9.4.
How can hosting configurations affect PHP error display?
10.
Conclusion 
Last Updated: Dec 8, 2024
Easy

How to Display Errors in PHP?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

When a PHP application is run, it displays several errors during the execution due to which it becomes important for the programmer to debug the error. Error reporting is a crucial part of any PHP application. By default, PHP might hide the error messages making it difficult to find the issues. Enabling PHP to display the error messages and warnings helps to identify and resolve the bugs efficiently. 

How to Display Errors in PHP?

Whenever a PHP application is executed, it generates multiple warnings and error messages. To debug those errors PHP provides us with different methods to display all the error messages. 

Below is the description for each of the methods to display the error in the PHP application. 

  1. error_reporting: It displays all the error level messages except E-STRICT, E-DEPRECATED, E-NOTICE level errors. 
     
  2. display_errors: Default value is off. Manually change it to on. 
     
  3. log_errors: By default, log_errors default value is ON which means whether error logging should be performed or not. 
     
  4. error_log string: This method sets the file name where the error messages be logged in. 

Quickly Show All PHP Errors

To display all the errors in the PHP application, add the below code in the PHP code file. This is the simplest and quickest way to get all the errors in the PHP code. 

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

 

  • ini_set: The ini_set function will override the configuration found in your PHP ini file. 
     
  • display_errors: It is a directive that determines whether the error message will be displayed to the user or will remain hidden. After the development, this directive is generally turned off. 
     
  • display_startup_errors: This display_startup_errors is a directive in PHP that is used to find the error during the startup sequence. There are multiple other directives that can be overridden using the ini_set function. To get more information on those directives, please refer to official documentation. 
     

These directives won’t be able to display parse errors like missing semicolons, or curly braces. To get information about these, you need to modify the PHP ini configuration file. 

How to Configure PHP.ini to Display All Errors and warnings? 

Even if you add the PHP code and still error message is still not visible, you may need to modify the code. You additionally need to make changes in the PHP ini configuration.  To get the PHP ini file, you must check the displayed output of phpinfo() function. There are directives that still need changes. 

display_errors = on 


You need to on the display_error directive as it will display all the errors like parse errors, syntax errors, or compile time errors. These cannot be displayed by calling the ini_set function in the PHP code. If the web application is in production, you must set this to off. 

Use of .htaccess file to Display PHP Error

Developers have access to the root or public directory of the project. The .htaccess file is located in the root directory so making changes in this file will also enable us to display PHP errors. 

Below is the code to add to the file 

php_flag display_errors on; 
php_flag display_startup_errors on; 


Both of these directives are also included in the .htaccess file. This file is different for the production and development environment. Since the file is different for both the environment will give an advantage to the developers. Developers can choose as to which environment the change must be made to display the error messages. Also, there are multiple hosting providers that do not allow to make changes in the PHP ini file configuration. 

error_reporting () Function

To control the level of error messages in the log file, developers make use of the error_reporting() function. This is a built-in PHP function that is used to specify which PHP errors are to be reported during the execution of the script. You can control what type of error, warnings, or notices should be displayed in the log file.  By setting it to true, it will display all the errors that will occur in the code. 

Syntax of error_reporting () Function

error_reporting (level)


Error Report level

Level: This parameter specifies at which level the error messages are displayed in the log file. It can be a combination of constant values or predefined levels. 

Below are some of the levels that can be used as a parameter in error_reporting function. 

Error TypeDescription
E_ERRORFatal runtime error that causes script termination.
E_WARNINGNon-fatal runtime error that causes script termination.
E_PARSECompile time error that is generated by the parser.
E_NOTICENoticed an issue that can cause an error. For example: an uninitialized variable.
E_CORE_ERRORFatal occurs that happens during the startup of the script.
E_USER_WARNINGNon-fatal error that is generated by the user and is similar to E_WARNING. PHP script also generates this error using the trigger function.
E_STRICTIt became part of E_ALL after PHP version 5.4.0 and it is not an error but it forces to follow all coding standards.
E_ALLIt enables all the error and warnings.

Using the error_log() Function to Send PHP Errors to a File 

When an application is in production, developers do not want error messages to be visible on the screen. You need to configure and modify the PHP settings to log errors to a file instead of showing it to the users. To achieve this, you set the log_errors directive to on and provide it with a file path using the error_log function. 


Either you can make the changes in the php.ini file or directly in your PHP script. 

Below is the code for implementing the error_log() function. 

Set the error logging in the script

ini_set('log script',1);
ini_set('error_log', '/path/log.file/error');

Set the error logging in php.ini file 


Open the php.ini file and update the settings

log_errors = on;
error_log = path/log.file/error


To apply these changes, you need to restart the web server. 

Modern Methods to Handle PHP Errors 

As PHP involves with time, modern functions and methods have evolved which helps to create robust and secure applications. Excluding the traditional methods and using these advanced methods helps to manage the errors and exceptions effectively. 

Method 1 - Using ini_set() for More Controlling Errors

Error handling using the ini_set() function helps to control error_reporting at the granular level. You can control the error messages as to which screen must see the error. The production screen will not be able to see the error messages, and those errors will be stored in the log file.

ini_set( 'display errors ', ' off’);


When this property is set to off, errors will not be displayed on the production screen. To make the errors visible set this property to on. 

ini_set( 'display errors ', ' on’);


You can also set the file path for the error message to be stored in that path file. 

ini_set('error_log', 'path_name');

Method 2 - Using Try-Catch to Handle Exceptions in PHP 8.x

Handling errors in PHP with try-catch block is best in terms of coding standards, especially for larger applications. From PHP 8.x onwards, errors can be handle using the try-catch block providing great efficiency to handle the errors and exceptions. 

try{
//code here
}catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(); 
error_log($e->getMessage());
}

Example

  • PHP

PHP

<?php

echo "Hello, World" // Missing semicolon will cause a parse error

?>
You can also try this code with Online PHP Compiler
Run Code

 

Output

Parse error: syntax error, unexpected '?>' in path_to_file on line x

Frequently Asked Questions

What is the difference between error_reporting and display_error in PHP?

Both of these methods have different functionality. The error_reporting() method allows you to determine which type of errors will be reported while display_error method is used to decide whether the error message should be visible or not. 

Why you should turn off display_error() method in the production environment? 

It is crucial to ensure that the display_error() method is turned off in the production environment as you do want the error message to be visible on the screen. Also you do not wish to show the important database queries and filepath to the users. 

What is the role of php.ini in error display?

The php.ini controls the error display setting in the PHP file. The php.ini does not display all kinds of errors. If you explicitly on the display_error directive it will display all the errors like parse errors, syntax errors, or compile time errors. 

How can hosting configurations affect PHP error display?

The hosting configuration affects the PHP error display. Developers have access to the root or public directory of the project. Using the settings in php.ini or .htaccess files developers can influence PHP error display. 

Conclusion 

In this blog, we thoroughly discussed how to display errors in PHP. Error reporting is crucial and important as it helps to debug the code faster. We looked how to display all the PHP errors and the modification needed in the files to show those error messages. We later looked at some of the modern practices to display those errors and examples for better understanding. 

Live masterclass