Debugging in PHP


When developing a website it helpful to have PHP display the errors. By default display_errors is set to false. While in development, it is helpful to turn this on. Just be sure to turn it back off after the web page complete and working properly. For a number of reasons, including security, users of the website should not have access to the error messages.

There are several ways this can be accomplished depending on the desired scope.

Individual web page:

At the top of the web page add the following code:

<?php
ini_set ('display_errors','On');
error_reporting(E_ALL);
?>

Entire web server:

Locate the php.ini file

Locate the following lines and make the change to on a on and E_ALL

Level of Error Reporting Set on Each page

Configure PHP.ini to display_errors=on

At the top of each page, set the level of error/message reporting with the following code:

<?php
    error_reporting(E_ALL);
?>

<?php
    error_reporting(E_ERROR | E_PARSE);
//Fatal and parse errors
?>

<?php
    error_reporting(E_ERROR &~ E_NOTICE);
//All but run-time notices
?>

Debugging Technique:

To view the PHP code in the browser window with the code highlighted, use the following to any file in the same directory.