Loading ...
Sorry, an error occurred while loading the content.

Error Handling in PHP

Expand Messages
  • Mahbubul Islam
    Error Handling An error represents an incorrect result, produced either because of wrong logic, such as division by zero, or improper working of other
    Message 1 of 1 , Jun 5, 2005
    • 0 Attachment

      Error Handling

      An error represents an incorrect result, produced either because of wrong logic, such as division by zero, or improper working of other applications, such as the database server that the PHP script is using. PHP displays the error message either on the Web page, or in the error log files. The error message contains information about the nature and source of an error.

      PHP has built-in support for error handling and classifies errors according to the following error types:

      • E_NOTICE: Represents minor nonfatal errors that you use to identify the possible bugs within the PHP script. These errors do not stop the execution of the PHP script. E_NOTICE errors are not displayed on the Web page by default.
      • E_WARNING: Represents nonfatal runtime errors that do not stop the execution of the PHP scripts. The errors generated by external applications, such as database servers, are represented by E_WARNING errors. These errors differ from E_NOTICE as the latter is associated with identifying bugs within the PHP script whereas the former is generated by external applications.
      • E_ERROR: Represents fatal errors that stop the execution of the PHP script.
      • E_PARSE: Represents parsing errors generated by the PHP parser, which indicate syntax error in the PHP script.

      In addition to the standard errors, PHP also provides custom error types that PHP scripts can generate using the trigger_error() function. The custom error types are:

      • E_USER_NOTICE: Represents the customized or user-defined version of the E_NOTICE error type.
      • E_USER_WARNING: Represents the customized or user-defined version of the E_WARNING error type.
      • E_USER_ERROR: Represents the customized or user-defined version of the E_ERROR error type.

      PHP provides built-in error handling functions that handle errors of different types, such as E_ERROR, E_WARNING. Some error handling functions are:

      • int error_reporting ( int err_type): Specifies what error types should be displayed on the Web page. The parameter, err_type, takes either integers or named constants to represent the error type. Following Table shows the named constants and their equivalent integer values:


      Named Constants and Equivalent Integer Values

      Named Constant

      Integer Value

















      • int error_log ( string message [, int message_type [, string destination ]]): Sends the error message to the specified location. The specified location can be the default system log file, an email address, a remote destination, or a local log file. The first parameter represents the error message that is stored in the specified destination. The second parameter represents the message type. The third parameter represents one of the four destinations where the error message needs to be stored. The second parameter can take one of the following values:
        • 0: Stores the error messages in the default system log file.
        • 1: Sends the error message to the destination email ID specified in the third parameter.
        • 2: Saves the error message in a file present on a different computer in a network. The third parameter specifies the IP address and the port number of the destination computer.
        • 3: Sends the error message to the destination file specified in the third parameter.


      • void trigger_error ( string error_msg [, int error_type]): Triggers user-defined warnings, notices, or errors. The first parameter represents the error message and the second parameter represents the error type that needs to be triggered.

      Listing 1

      shows how to trigger errors using the trigger_error() function.

      Listing 1

      : Generating Errors Using the trigger_error() Function


      // set the error reporting level for this script


      if (assert($divisor == 0))


      error_log("Cannot perform division by zero", 3, "var/www/html/PHP/error.log");

      trigger_error("Cannot perform division by zero", E_USER_ERROR);




      The above listing generates an error that stops the execution of the script if the condition passed to the assert() function is true. In the above listing:

      • The assert() function checks whether the divisor is zero or not.
      • The error_log() function sends the message, Cannot perform division by zero, to the log file specified by the second parameter when the divisor is zero.

      Save the above listing as Trigger.php in the PHP folder.

      You can also define your own custom error handlers. You need to define a function that acts as an error handler. The syntax to declare a custom error handling function is:

      function func_name($errorno, $errorstr [,$errorfile, $errorline, $errorcontext])

      The above syntax shows how to declare an error handling function. In the above code:

      • The func_name represents the name of the error-handling function.
      • The function must accept at least two parameters and a maximum of five parameters.
      • The first two parameters represent error number and error description. The remaining parameters are optional.
      • The third parameter represents the file name in which the error occurred.
      • The fourth parameter represents the line number, and the fifth parameter represents the context in which the error occurred.

      After defining an error handling function, you need to set it as the error handler using the set_error_handler() function. This function takes the name of the error handling function as a parameter.

      Listing 2

      shows how to use a custom error handler:

      Listing 2:

      Using Custom Error Handler



      function ErrHandler($errorno, $errorstr, $errorfile, $errorline)


      $display = true;

      $notify = false;

      $halt_script = false;

      $error_msg = "<br>The $errorno error is occurring at $errorline in




      case E_USER_NOTICE:

      case E_NOTICE:

      $halt_script = false;

      $notify = true;

      $label = "<B>Notice</B>";


      case E_USER_WARNING:

      case E_WARNING:

      $halt_script = false;

      $notify = true;

      $label = "<b>Warning</b>";


      case E_USER_ERROR:

      case E_ERROR:

      $label = "<b>Fatal Error</b>";


      $halt_script = false;


      case E_PARSE:

      $label = "<b>Parse Error</b>";


      $halt_script = true;



      $label = "<b>Unknown Error</b>";





      $msg = $label . $error_msg;

      echo $msg;


      if($halt_script) exit -1;


      $error_handler = set_error_handler("ErrHandler");

      echo "<BR><H2>Using Custom Error Handler</h2><BR>";

      trigger_error("<BR>Error caused by E_USER_NOTICE</BR>", E_USER_NOTICE);

      trigger_error("<BR>Error caused by E_USER_WARNING</BR>", E_USER_WARNING);

      trigger_error("<BR>Error caused by E_USER_ERROR</BR>", E_USER_ERROR);

      trigger_error("<BR>Error caused by E_PARSE</BR>", E_PARSE);



      The above listing defines a custom error handler function, ErrHandler(), which handles the error generated by the trigger_error() function. In the above listing:

      • The $display variable is a configuration flag that indicates whether to display the error on the Web page or not. The configuration flags are used for the proper functioning of PHP scripts.
      • The $notify variable is another configuration flag that specifies whether the error should be notified to the end user or not. The default value for the $notify variable is false.
      • The $halt_script configuration variable specifies whether to stop the execution of the PHP script when a fatal error occurs. The default value of the $halt_script variable is true.
      • The $error_msg variable represents the error message to be displayed when an error occurs.
      • The switch statement execute statements for any error type based on the value of the $error variable.
      • The $notify variable displays the error message.
      • The ErrHandler() function is set as the error handler using the set_error_handler() function.
      • The trigger_error() function triggers the error of each of the error types.


      The trigger_error() function can only trigger error of the E_USER types. It cannot trigger error for standard error types. Save the above listing as Trigger2.php.


      Reference :: Integrating PHP and XML

      SkillSoft Press © 2004 )



      Do You Yahoo!?
      Tired of spam? Yahoo! Mail has the best spam protection around

    Your message has been successfully submitted and would be delivered to recipients shortly.