PHP Error Reporting

Turning on Error Reporting in PHP

Man using laptop in the living room
Gehringj/E+/Getty Images

If you are running into a blank or white page or some other PHP error, but you have no clue what is wrong, you should consider turning on PHP error reporting. This gives you some indication of where or what the problem is, and it is a good first step to solving any PHP problem. You use the error_reporting function to turn on error reporting for a specific file that you want to receive errors on, or you can enable error reporting for all your files at your web server by editing the php.ini file.

This saves you the agony of going over thousands of lines of code looking for an error.

Error_reporting Function

The error_reporting() function establishes the error reporting criteria at runtime. Because PHP has several levels of reportable errors, this function sets the desired level for the duration of your script. Include the function early in the script, usually immediately after the opening <?php. You have several choices, some of which are illustrated below:

<?php

//Report simple run errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

//Report E_NOTICE in addition to simple run errors
//(to catch uninitialized variables or variable name misspellings)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

//Report all PHP errors
error_reporting(-1);

//Report all PHP errors (see changelog) 
 error_reporting(E_ALL);

//Turn off all error reports
error_reporting(0);

?>

How to Display Errors

Display_error determines whether errors are printed on the screen or hidden from the user.

It is used in conjunction with the error_reporting function as shown in the example below:

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

Changing the php.ini File at the Website

To see all error reports for all your files, go to your web server and access the php.ini file for your website. Add the following option:

 error_reporting=E_ALL

The php.ini file is the default configuration file for running applications that use PHP. By placing this option in the php.ini file, you are requesting error messages for all your PHP scripts.