Last Updated:

How to display PHP errors on the screen

Errors in the PHP script are a common thing not only for beginners, but also for professionals. Let's figure out how to find out that our code is not working correctly, and fix it.

PHP errors on the screen

What are the mistakes

They can be divided into 2 types: fatal and non-fatal. The first type implies that the problem is so serious that after it occurs, the script ceases to run, and the second type can cause misunderstandings in the further execution of the code and contribute to the occurrence of new failures.

For example, a fatal error would be the appearance on the screen of the inscription E_ERROR, which may mean, for example, an incorrect allocation of memory. And the occurrence of E_NOTICE will not affect the execution in any way, but will warn the developer that his code takes a long time to load. This issue may occur if the code is in order incorrectly or when large third-party files are attached.

A complete list of constants and their values can be found in the official php documentation in the predefined constants section. They can be used in functions to output specific kinds of errors.

Enable errors to appear in the script itself

The easiest way, available even to a beginner, is to write a line at the beginning of the script:

error_reporting (‘E_ALL’);

It is written before the main code is executed and in case of any failures will display their values on the screen. Instead of E_ALL, you can write any constants of interest.

In the .htaccess file

This option only works on Apache servers. The .htaccess file is a plain text document that is needed to easily configure the server and specify, for example, what will happen if the user goes to the page with an error. Their output is configured by prescribing a line in the file:

php_flag display_errors On;

To specify the specific type of errors that you want to display, write:

php_value error_reporting “E_ALL”;

In the php file.ini

This parameter has the highest execution priority, followed by information from .htaccess and only after that the code itself. It applies to all sites on the server, so it is more often used by administrators.

In php.ini, the output function is first prescribed, and after it:

display_errors On;

For the change to take effect, be sure to restart the server.

How to fix the code

To fix all the problems that cause errors, the code should be carefully checked. To do this, you can include alternate output in the php file. Before each individual block of php code, they write:

ini_set (‘display_errors’, ‘On’);

After that, enable the display of all or some types of errors:

error_reporting (‘E_ALL’);

When the code block being checked is finished, you need to write the function again ini_set, but with a value of Off.

Do not use the @ operator before expressions. It suppresses failures without displaying them on the screen, but the problem does not disappear from this and can cause problems with the script in its subsequent parts.

How to disable display for a user


If errors occur during code execution, only developers need to see them. While the site is in the process of development, it is acceptable to display error constants on the page, but the average user does not need to see them.

To collect all the logs of the site, create a special PHP_errors.log file in the root folder, and write the function in the script:

php_value error_log .. /PHP_errors.log

At the same time, all the error display parameters previously prescribed in the code are changed from On to Off.