Use of Error Handling in PHP

Introduction

Before explaining PHP error handlers, I am describing what an error is.

What is an Error

"An Error is an expected or unexpected event that occurs when your PHP code is running." Now I am explaining PHP error handling.

Error Handling in PHP

Errors are a most common event that a developer faces during programming and every well-constructed PHP application should have an error handler. The default error handling in PHP is very simple.

Example of default error handling

<?php

if(!file_exists("test.txt"))

{

 

}

else

{

fopen("test.txt");

print"Opend file sucessfully"

}
?>

 

Note: The default error includes filename and line number.

Output

default-error-handling-in-php.jpg
 

Basically, error handling is a process of catching errors, which is raised by your programs. The default error handling for PHP is shown in the above example. You should handle errors as in the following error handling methods.

  • With the use of the die() function.

  • With the use of a custom error and error triggers.

  • With the use of error reporting.

There are some other error handler functions as shown in this table:

 

Name Type Description
display_error string It determines if errors are displayed or hidden.
display_startup_errors boolean Even if display_errors is on, hides errors that occur during PHP startup sequence.
log_errors boolean It specifies if script error messages should be logged to the servers error log.
log_errors_max_len integer It specifies maximum length of log_errors in bytes
ignore_repeated_errors boolean Do not log repeated messages.
ignore_repeated_source boolean It ignores the source of a message when ignoring repeated messages.
report_memleaks boolean If set to Off, memory leaks will not be displayed.
track_errors boolean If it is enabled, variable will always hold the last error message.
html_errors boolean It turns off HTML tag in error message.
xmlrpc_error_number integer It is used as value of the XML-RPC fault code element.
error_prepend_string string It shows string to output before an error message.
error_appen_string string It shows string to output after an error message.
error_log string It write name of the file where script errors should be logged.


Some error handling functions are described here.

PHP error handling with die() function
 

You can explicitly handle errors by the die() function. While writing your PHP program you should check all possible error conditions before proceeding and take appropriate action when required.

Example of die() function
 

<?php

if(!file_exists("test.txt"))

{

die("<b>File not Found.</b>");

}

else

{

fopen("test.txt");

print"Opend file sucessfully";

}
?>


Output

die-in-php.jpg

PHP error handling with custom error handler function


You can create your custom error handler function for handling errors in PHP.


Syntax of custom error handler
 

error_function(error_level, error_message, error_file, error_line, error_context )

In the custom error handler two parameters must be defined and others are optional.

Parameter in custom error handler

There are the following parameters in the custom error handler:

Parameter Description
error_level Requird, it specifies the error report level for a user defined error.
error_message Required, it specifies the error message for a user defined error
error_file Optional, it specifies the filename in which the error fired.
error_line Optional, it specifies the line number in which the error fired.
error_context Optional, it specifies an array containing every variable, and their values in use when the error is fired.

Example of  custom error handler

<?php

functionCoustomErrorHandler($errno,$errmsg)

{

echo"<b>Error</b>: [$errno] $errmsg";

}

set_error_handler("CoustomErrorHandler");

echo($var);
?>


Output

error-handler-in-php.jpg

PHP error handling with Trigger an error

This function can be used either along with the built-in error handler or with a user defined function which is set as a new error handler.

Syntax of trigger error function
 

trigger_error( error_msg, error_type)

Parameter in trigger_error function

There are the following parameters in the trigger_error function:

Parameter Description
error_msg Requird, A string which is displayed as error message.
error_type Optional, It specifies the error type for an error message.

Example of  trigger_error()

<?php

 

functionCustomErrorHandler($errno, $errstr)

  {

  echo"<b>Error:</b> [$errno] $errstr<br>";

  echo"Ending Script";

  die();

  }

 

set_error_handler("CustomErrorHandler",E_USER_WARNING);

 

 

$val=2;

if ($test>=1)

  {

  trigger_error("Value must be 1 or below",E_USER_WARNING);

  }
?>


Output

trigger-an-error-in-php.jpg 


PHP error handling with error_reporting() function


The error reporting function is used to set which PHP errors are reported.
 

Syntax of error reporting function
 

error_reporting(level)

Parameter in error_reporting function

There are the following parameters in the trigger_error function:

Parameter Description
level Optional, It specifies the error report level of a script.

Example of  error_reporting()

<?php

error_reporting(0);

error_reporting(E_ERROR | E_WARNING | E_PARSE);

error_reporting(E_ALL);
?>

 


Similar Articles