How to display PHP errors?

On a production server, you will definitely want to turn off errors from displaying on the screen due to security and usability reasons but what do you do when you're on a development machine trying to debug a malfunctioning PHP script? PHP gives a programmer an ability to control logging of errors, and the types of errors to be displayed. You may enable PHP error display in one of two ways.

1. You may turn on error display at the script level when a server level error display is turned off.

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

** Although display_errors may be set at runtime (with ini_set('display_errors', 1)), it won't display anything if the script has fatal errors.

To display fatal errors, you'll need to register a shutdown function with the register_shutdown_function.

register_shutdown_function('shutdown');
function shutdown() {
	print_r(error_get_last());
}

2. You may turn on error display at the server level by editing the php.ini file. The location of php.ini varies depending on the flavor of Linux you're running. On CentOS/RedHat, it is located in /etc folder, and on SuSE Linux the php.ini is located in /etc/php5/apache2 folder.

display_errors = On
error_reporting = E_ALL & ~E_NOTICE

3. You may suppress error at a line level with an @ operator. By having the @ operator on an expression, the error will be suppressed at the line level.

Here is the runtime configuration stored in the php.ini with the default settings.

; error_reporting is a bit-field. Or each number up to get desired error
; reporting level
; E_ALL – All errors and warnings (doesn’t include E_STRICT)
; E_ERROR – fatal run-time errors
; E_WARNING – run-time warnings (non-fatal errors)
; E_PARSE – compile-time parse errors
; E_NOTICE – run-time notices (these are warnings which often result
; from a bug in your code, but it’s possible that it was
; intentional (e.g., using an uninitialized variable and
; relying on the fact it’s automatically initialized to an
; empty string)
; E_STRICT – run-time notices, enable to have PHP suggest changes
; to your code which will ensure the best interoperability
; and forward compatibility of your code
; E_CORE_ERROR – fatal errors that occur during PHP’s initial startup
; E_CORE_WARNING – warnings (non-fatal errors) that occur during PHP’s
; initial startup
; E_COMPILE_ERROR – fatal compile-time errors
; E_COMPILE_WARNING – compile-time warnings (non-fatal errors)
; E_USER_ERROR – user-generated error message
; E_USER_WARNING – user-generated warning message
; E_USER_NOTICE – user-generated notice message
;
; Examples:
;
; – Show all errors, except for notices and coding standards warnings
;

error_reporting = E_ALL & ~E_NOTICE

; Print out errors (as a part of the output). For production web sites,
; you’re strongly encouraged to turn this feature off, and use error logging
; instead (see below). Keeping display_errors enabled on a production web site
; may reveal security information to end users, such as file paths on your Web
; server, your database schema or other information.

display_errors = On

Note: PHP 5.3 or later, the default error_reporting value is E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED. This setting does not show E_NOTICE, E_STRICT and E_DEPRECATED level errors. Prior to PHP 5.3.0, the default value is E_ALL & ~E_NOTICE & ~E_STRICT. In PHP 4 the default value is E_ALL & ~E_NOTICE. To catch all errors during the development, "E_ALL" value is recommended.

NOTICE messages will warn you about possible bugs in your code. For 
example,  use of unassigned values is warned. It is extremely useful to 
find typos and to save time for debugging. NOTICE messages will warn 
you about bad style. For example, $arr[item] is better to be written as 
$arr['item'] since PHP tries to treat "item" as constant. If it is not a
constant, PHP assumes it is a string index for the array.
In PHP 5, a new error level E_STRICT is introduced. Prior to PHP 5.4.0 
E_STRICT was not included within E_ALL, so you would have to explicitly
enable this kind of error level in PHP < 5.4.0. STRICT messages provide
suggestions that can help ensure the best interoperability and forward 
compatibility of your code. These messages may include things such as 
calling non-static methods statically, defining properties in a compatible 
class definition while defined in a used trait, and prior to PHP 5.3 some 
deprecated features would issue E_STRICT errors such as assigning 
objects by reference upon instantiation.
Tags: 

Comments

A good programming practice may include debugging option in your header, and only display errors when the script is running in debug mode. Please see an example below:

define('__DEBUG__', FALSE);
if (__DEBUG__) {
	ini_set('display_errors', 1);
	error_reporting(E_ALL && ~E_NOTICE);
}
By admin

If you have display_errors turned on in your script but seeing a blank page ("White Screen of Death") instead of the PHP errors; you may have PHP parsing error(s). PHP errors are reported at run-time, but the syntax errors are caught before script execution causing blank screen. You may have left out single quote ', curly brace }, or semicolon ; somewhere in your script.

To catch those parsing errors, you'll have to use server level or folder level error reporting. To setup server level error reporting, you'll have to edit php.ini file; and to configure folder level error reporting, you'll edit .htaccess file.

Edit php.ini

display_errors = On
error_reporting = E_ALL & ~E_NOTICE

Edit .htaccess:

php_value display_errors 1

To easily detect syntax errors during development, color coded editors will help you catch syntax errors before even running it. My personal preference is Eclipse-PHP (or eclipse with php plug-in).

By admin

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.