blog

Home / DeveloperSection / Blogs / Error Handling in PHP

Error Handling in PHP

Anonymous User 1618 09-Jun-2016

Error handling is the process of catching error and then taking appropriate action. When you create web application error handling is an important part. If your code lacks error checking code, your program look unprofessional and it will open to security risks.

Here are some different types of error handling methods:

·    Use simple ‘die’ function.

·    Custom error and error trigger

·     Error reporting

Use die function

While writing your program you should check all possible condition and take appropriate action. Below example will just open the file.

<?php
$file=fopen("team.txt","r");
?>

 If there is no file “team.txt” you might get an error like this:


Warning: fopen(team.txt) [function.fopen]: failed to open stream:
No such file or directory in C:\phpfolder\error.php on line 2

Note: To prevent the user from getting this error message use die statement for handling errors as done in below example:

<?php

if(!file_exists("team.txt")) {
  die("File cannot be found");
} else {
  $file=fopen("team.txt","r");
}
?>

  Output: 

File cannot be found

Note: It was a simple error handling mechanism to stop the program. However to stop the program is not always right way to go. Let’s take an alternative PHP function for error handling.

Creating a custom error Handler

You can write your own function for handling errors.PHP provides framework to define error handling. We simply create a function and call this function when an error occurs.

Syntax:
          error_function(error_level,error_message, error_file,error_line,error_context); 

Note:It this function two parameter is required(error_level, error_message) and others are optional(error_file , error_line, error_context).

Parameter

Description

error_level

Specifies the error report level for the user-defined error and it must be a value number.(Required)

error_message

Specifies the error message for the user-defined error.(Required)

error_file

Specifies the name of the file in which error occurred (optional).

error_line

Specifies the line number in which error occurred (optional).

error_context

Specifies an array containing variables and their values when the error occurred.(optional)

 

Error report level

These error report levels are the different types of error. They are given below:

Value

Constant

Description

2

E_WARNING

Non-fatal run-time errors. Execution of the script is not halted.

8

E_NOTICE

Run-time notices. The script found something that might be an error.

256

E_USER_ERROR

Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error().

512

E_USER_WARNING

Non-fatal user generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error

1024

E_USER_NOTICE

User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error

4096

E_RECOVERABLE_ERROR

Catchable fatal error. This is alike an E_ERROR but can be caught by a user defined handle.

8191

E_ALL

All errors and warnings, except level E_STRICT.


Updated 15-Mar-2018
I am a content writter !

Leave Comment

Comments

Liked By