PHP.nl

Basics

Basics

PHP reports errors in response to a number of internal error conditions. These may be used to signal a number of different conditions, and can be displayed and/or logged as required.

Every error that PHP generates includes a type. A is available, along with a short description of their behaviour and how they can be caused. list of these error types

Handling errors with PHP

If no error handler is set, then PHP will handle any errors that occur according to its configuration. Which errors are reported and which are ignored is controlled by the

php.ini directive, or at runtime by calling . It is strongly recommended that the configuration directive be set, however, as some errors can occur before execution of your script begins. error_reportingerror_reporting

In a development environment, you should always set

to , as you need to be aware of and fix the issues raised by PHP. In production, you may wish to set this to a less verbose level such as , but in many cases is also appropriate, as it may provide early warning of potential issues. error_reportingE_ALL``E_ALL & ~E_NOTICE & ~E_DEPRECATED``E_ALL

What PHP does with these errors depends on two further php.ini directives.

controls whether the error is shown as part of the script's output. This should always be disabled in a production environment, as it can include confidential information such as database passwords, but is often useful to enable in development, as it ensures immediate reporting of issues. display_errors

In addition to displaying errors, PHP can log errors when the

directive is enabled. This will log any errors to the file or syslog defined by . This can be extremely useful in a production environment, as you can log errors that occur and then generate reports based on those errors. log_errorserror_log

User error handlers

If PHP's default error handling is inadequate, you can also handle many types of error with your own custom error handler by installing it with . While some error types cannot be handled this way, those that can be handled can then be handled in the way that your script sees fit: for example, this can be used to show a custom error page to the user and then report more directly than via a log, such as by sending an e-mail. set_error_handler