w3schools Logo
Thursday 23 February 2012
www.w3schools.in Developer Network
HTML tutorials
CSS tutorials
php tutorials and scripts
MySql tutorials
Learn C Programming
Operating system

php error handling
From w3schools.in

Errors and ErrorManagement

Errors are an integral part of every computer language—although one that, most of the time, programmers would rather not have to deal with! PHP has some excellent facilities for dealing with errors that provide an excellent level of fine-grained control over how errors are thrown, handled and reported.

Proper error management is essential towriting applications that are both stable and capable of detecting when the inevitable problem arises, thus handling failure in a graceful manner.

Types of Errors

There are several types of errors—usually referred to as error levels in PHP:

  •  Parse error: A problem with the syntax of your program, such as leaving a semicolon off of the end of a statement. The interpreter stops running your program when it encounters a parse error.
     
  •  Fatal error: A severe problem with the content of your program, such as calling a function that hasn't been defined. The interpreter stops running your program when it encounters a fatal error.
     
  •  Warning: An advisory from the interpreter that something is fishy in your program, but the interpreter can keep going. Using the wrong number of arguments when you call a function causes a warning.
     
  •  Notice: A tip from the PHP interpreter, playing the role of Miss Manners. For example, printing a variable without first initializing it to some value generates a notice.
     
  •  Strict notice: An admonishment from the PHP interpreter about your coding style. Most of these have to do with esoteric features that changed between PHP 4 and PHP 5, so you're not likely to run into them too much.

Error Reporting

By default, PHP reports any errors it encounters to the script’s output. Unless you happen to be in a debugging environment, this is rarely a feature that you will want to take advantage of: allowing users to see the errors that your scripts encounter is not just bad form—it could be a significant security issue.

Luckily, several configuration directives in the php.ini file allow you to fine-tune how—and which—errors are reported.
The most important ones are error_reporting, display_errors and log_errors.

The error_reporting directive determines which errors are reported by PHP. A series of built-in constants allow you to prevent PHP from reporting errors beneath a certain pre-defined level. For example, the following allows for the reporting of all errors, except notices:

error_reporting=E_ALL & ~E_NOTICE

Error reporting can also be changed dynamically from within a script by calling theerror_reporting() function.

The display_errors and log_errors directives can be used to determine how errors are reported. If display_errors is turned on, errors are outputted to the script’s output; generally speaking, this is not desirable in a production environment, as everyone will be able to see your scripts’ errors. Under those circumstances, you will instead want to turn on log_errors, which causes errors to be written to your web server’s error log.

Handling Errors

Your scripts should always be able to recover from a trappable error—even if it’s just to advise the user that an error occurred and notify support staff of the same fact. This way, your script won’t simply capitulate when something unexpected
occurs—resulting in better communication with your customers and the possible avoidance of some major problems.
Luckily, error handling is very easy. Your scripts can declare a catch-all function that is called by PHP when an error condition occurs by calling theset_error_handler() function:

$oldErrorHandler = ’’;
      function myErrorHandler ($errNo, $errStr, $errFile, $errLine, $errContext) {
                 logToFile("Error $errStr in $errFile at line $errLine");
                  // Call the old error handler
                  if ($oldErrorHandler) {
                  $oldErrorHandler ($errNo, $errStr, $errFile, $errLine, $errContext);
            }
      }
$oldErrorHandler = set_error_handler ($oldErrorHandler);

As you can see, the function name of the old error handler (if any) is returned by the call to set_error_handler()—this allows you to stack several error handlers on top of each other, thus making it possible to have different functions handle different kinds of errors.
It’s important to keep in mind that your error handlerwill completely bypass PHP’s errormechanism—meaning that you will be responsible for handling all errors, and stopping the script’s execution if necessary.


This page was last modified on 18 Nov 2010 at 14:06:24.
Gautam Kumar
EDP Manager
http://www.w3schools.in
Related Posts
» Useful functions to tighten the security in PHP
» CodeIgniter A recommendation for PHP Programmer
» PHP Framworks Why when and which
» Solving Floating point number precision lost problem in PHP
» Handling array of HTML Form Elements in JavaScript and PHP
» How to filter user submitted data easily in PHP
» Prevent form post request from another domain in PHP
» Flaw in and or logical operator php
» Default arguments functions php
» Submit Form without Refreshing Page with Jquery
» MySQL Performance Tips
» Free ajax chat applications in PHP
» Return More Than One Value From a Function in PHP
» PHP Optimization Tips
» PHP Error Handling
» Useful PHP Classes and Libraries
» Useful Tools For PHP Developers
» PHP Frameworks
» Web based HTML Editors
» 301 redirect in PHP and .htaccess
» Hiding PHP file extension
» Rredirect Browser HTTPS SSL PHP
» Tighten php security functions
» PHP Framworks