How to display errors in PHP

Displaying Error Messages in PHP

This assumes you have already installed PHP on your computer. By default, most PHP installations will not display error messages in the browser. This is a security feature. However, during development it's a good idea to display error messages.

PHP5 is supposed to allow you to override the system-wide configuration settings by including a php.ini file in your current working directory (the directory that contains you script). However, there appears to be a bug in PHP 5.1 as described in this bug report. Apparently, PHP 5.1 just ignores (or overrides) the settings in the local php.ini. To get around this problem, you can either modify the system-wide php.ini file, as described below, or you can use the work around described here.

Work Around Procedure (For runtime errors only)

Create a file named containing the following PHP code:

//error_reporting(E_ALL & ~E_NOTICE | E_STRICT); // Warns on good coding standards
  ini_set("display_errors", "1");

Place this file in a special include directory that is accessible to your PHP scripts and then place following command at the beginning of any PHP scripts where you want error messages displayed:

  include_once "../include/";

This example assumes that the include directory is contained in the script's parent directory.

Set display_errors=On in the System php.ini

This procedure will work for syntax and semantic errors.

1. Find the PHP configuration file php.ini on your system. (On the Mac it is: /usr/local/php5/lib/php.ini)

On Macintosh or Linux: $locate php.ini

2. Edit php.ini and search for the these lines (which will not be consecutive):

error_reporting = E_ALL
display_errors = Off

and change them to:

error_reporting = E_ALL & ~E_NOTICE | E_STRICT
display_errors = On

3. Restart Apache:

$ sudo apachectl restart