Nov 072013
Article PHP

This post lists several options for the handling of warning and error messages that can be thrown during the execution of a CGI script written in PHP.

Configuration files

The handling of error messages in PHP is controlled by directives in the configuration file php.ini.

The exact location of this file depends on the type of web server+PHP installation in our server. In a linux system, it is typically one of:

/etc/php5/apache2/php.ini .- For apache+mod_php installation.

/etc/php5/cgi/php.ini .- For apache+mod_fcgid and other setups where php scripts are executed as standard CGI scripts

/etc/php5/cli/php.ini .- This is the configuration file used when the script is executed from the command line.

Specifying the types of messages to be reported

The types of errors and warnings that we want to be written to the log file, or displayed on screen, are specified in the “error_reporting” directive. The recommended values for this directive are:

Sending messages to the screen or to an error log file

The default behaviour for PHP is to print the errors/warnings as part of the output page being generated, and therefore they are displayed in the client browser.

This can be very handy while a script is being debugged in a local test environment, but it is not desirable for a production website. Not only because of the poor impression that the user perceives about the quality of the site, but also because it can reveal sensible information that could be used by a hacker to attack our server.

The destination of the messages is established in the display_errors directive, as follows:


Display errors in the screen

Default: display_errors = On

On a production server, display_errors should be set to Off, while in a development server it is usually desirable to leave it as On.


Other error handling control directives

The other directives for the handling of error messages that can be used in the configuration file are:


Formats as html the error messages to improve their readability

Default: html_errors = On

This parameter is only take into account if  “display_errors = On” has been selected.

If the PHP manual has been installed (it can ben downloaded from, and the parameter “docref_root” has been edited to point to its location, the error messages are turned into links pointing to the page that explains the possible causes of the error.

Other related parameters are “error_prepend_string” and “error_append_string”. These vcan bre used to add format specifiers for the error messages. For instance, to make error messages be highlighted in red color:


Send the error messages to a log file

Default: log_errors = Off

Even if the default value for this parameter is Off, it is desirable to set this to On, both on the developer and production environments.

For this parameter to be effective, the value of the “error_log” parameters also needs to be specified, as explained below.


Specifies the log file where error messages are recorded. For example:

The value of this parameter can also be set to “syslog”. On a Unix/Linux system, error messages are then sent to the system log, while on a Windows system they are sent to the “Event Log”.

If the log file is not specified as an aboslute path, it is considered relative to the directory where the script is located. That is, the file “php_errors.log” in the example above will be created in the same directory where the script that generates an error is located.

On the other hand, if the error_log parameter is not specified, aor the log_errors parameters is set to Off, error are written to the generic error log file of the web server (usually in the path “/var/log/apache2/error_log”).


Print to screen errors produced during the initialization of the PHP interpreter, before the PHP script starts to be executed (for instance, when trying to load a module).

Default: display_startup_errors = Off

Specifying the parameters inside the script

The two built-in functions error_reporting() and ini_set() can be used to modify the values of the configuration parameters from inside the script being executed. This can be useful in cases where it is not possible to edit the configuration file, as may happen, for instance, if our web site is running in a shared hosting.


Sending messages to the log file

The error_log() function is also available to write ad hoc messages to the error log file (either the one specified in the “error_log” directive of the configuration file, or else the generic error log of the web server).

Besides, with additional arguments, the  error_log() function can also be used to send error messages through email, or to write to a different log file.




The complete reference to the error_log() function is available at

Index of posts related to PHP programming

 Posted by at 11:50 am

 Leave a Reply