Team LiB
Previous Section Next Section

The PHP Error-Handling Model

There are two basic types of errors in PHP: the traditional, procedural error, which has been part of the PHP framework since the beginning, and a new OOP- (object-oriented programming) based exception handling system introduced with PHP5. Although exceptions provide a greater degree of flexibility in handling deliberately raised errors generated by your script, you will undoubtedly need to be familiar with the simpler procedural error system so that you can effectively handle errors generated by PHP's sizeable library of non-OOP functions.

Error Types

PHP's error system defines 12 unique types of errors, which can be summarized into three basic categories: Informational, Actionable, and Fatal. Each can occur at any time from startup and compilation to runtime.

Informational errors are not truly "errors" as such, but rather the engine trying to let you know that although it can process the source code you've provided, something seems amiss and the referenced code should be looked at. Examples of informational errors include undefined constants, attempting to read an undefined variable, defining a class property in PHP5 using var instead of public, protected, or private, and about 200 other less-common cases. Most informational errors can be avoided through the use of explicit programming techniques, but are ultimately harmless to the execution of a script. Informational errors include the following:

E_STRICT

The current script relies on a feature or behavior that is deprecated and may not work in future versions of PHP. This error code is used only in PHP5 and later.

E_NOTICE

The compiler has detected a situation that could indicate a problem but that may in fact be normal.

E_USER_NOTICE

Identical to E_NOTICE in severity, but specifically raised through the use of the trigger_error() function in a PHP script. E_NOTICE errors, by contrast, are raised by the internals of the PHP engine.


Actionable errors indicate that something clearly wrong has happened and that your script may need to alter its behavior or even back out of its current processing and exit with an informative message to the user. These types of errors occur when expected resources are unavailable (file not found, database not responding, and so on), data passed to a function is outside of the expected range of values, when security settings prevent the current script from performing a specific action, or in more than 1,000 other situations. Actionable errors include the following:

E_WARNING

An actionable error that occurred during the execution of a built-in runtime function or block of code.

E_USER_WARNING

As with E_USER_NOTICE, this is the script-issued counterpart to E_WARNING and shares the same degree of severity.

E_COMPILE_WARNING

Raised in only a small set of situations, these errors occur during script compilation and usually relate to unexpected characters in the input file or unterminated multiline comments.

E_CORE_WARNING

Indicates an error during initialization of the PHP engine, loading of an external shared module, or a recoverable inconsistency in the engine environment.


Fatal errors occur when something so terrible has happened during the execution of your script, or during the startup of the PHP interpreter, that further processing simply cannot continue. When a fatal error occurs, PHP displays and/or logs an error message (depending on php.ini settings), and stops execution of your script. A fatal error may occur because PHP was unable to load a required module, the requested script could not be correctly read, or an instruction was encountered that could not be processed (for example, calling an undefined function). Fatal errors include the following:

E_ERROR

An internal function or block of code in the PHP engine encountered a condition which it could not recover from.

E_USER_ERROR

Raised by a script using trigger_error() to indicate that an unrecoverable error has occurred and PHP should halt execution after handling the error message.

E_COMPILE_ERROR

A critical error occurred while reading a script and preparing to parse it.

E_CORE_ERROR

Occurs when the PHP engine is unable to startup, shutdown, or load/unload a dynamic module.

E_PARSE

Raised during the compile phase in response to a syntax error.


    Team LiB
    Previous Section Next Section