---
title: "Error Handling in PHP"  
description: "Error handling is the process of catching error and then taking appropriate action. When you create web application error handling is an important par"  
author: "Anonymous User"  
published: 2016-06-09  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11149/error-handling-in-php  
category: "php"  
tags: ["php", "error"]  
reading_time: 3 minutes  

---

# Error Handling in PHP

[Error handling](https://www.mindstick.com/forum/160168/error-handling-in-go) is the process of catching error and then taking appropriate action. When you create [web application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about) error handling is an important part. If [your code](https://www.mindstick.com/forum/157976/how-can-you-use-jquery-s-testing-framework-such-as-qunit-to-write-unit-tests-for-your-code) lacks error checking code, [your program](https://answers.mindstick.com/qa/33494/how-do-you-find-any-view-element-into-your-program-explain-for-example) look unprofessional and it will open to [security risks](https://www.mindstick.com/forum/160411/what-security-risks-are-associated-with-bearer-tokens-and-how-can-they-be-mitigated).

**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](https://www.mindstick.com/forum/23174/error-message-the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configuration) 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](https://www.mindstick.com/forum/160113/how-does-the-api-s-global-exception-handling-mechanism-work-and-when-is-it-useful) 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](https://answers.mindstick.com/qa/94996/how-do-i-fix-a-fatal-error-in-windows-10). This is alike an E_ERROR but can be caught by a [user defined](https://www.mindstick.com/forum/34125/how-to-find-list-of-all-user-defined-tables-using-sql-server) handle. |
| ## 8191 | E_ALL | All errors and warnings, except level E_STRICT. |

---

Original Source: https://www.mindstick.com/blog/11149/error-handling-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
