WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP Error Handling


The default mistake dealing with in PHP is basic. A mistake message with filename, line number and a message depicting the mistake is sent to the browser.


PHP Error Handling

When making contents and web applications, mistake dealing with is an imperative part. On the off chance that your code needs mistake checking code, your program may look very amateurish and you might be available to security risks.

This instructional exercise contains probably the most well-known blunder checking techniques in PHP.

We will indicate distinctive blunder taking care of methods:

  • Simple "die()" statements
  • Custom mistakes and blunder triggers
  • Error reporting

Basic Error Handling: Using the bite the dust() function

The first model demonstrates a straightforward content that opens a content file:

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

If the document does not exist you may get a blunder like this:

Warning: fopen(notebook.txt) [function.fopen]: neglected to open stream:
No such document or index in C:\myfolder\test.php on line 2

To keep the client from getting a blunder message like the one above, we test whether the document exist before we endeavor to get to it:

<?php
if(!file_exists("notebook.txt")) {
  die("File not found");
} else {
  $file=fopen("notebook.txt","r");
}
?>

Now if the document does not exist you get a mistake like this:

Record not found

The code above is more productive than the prior code, since it utilizes a basic blunder dealing with system to stop the content after the error.

However, essentially halting the content isn't generally the correct approach. How about we take a take a gander at option PHP capacities for taking care of errors.


Creating a Custom Error Handler

Creating a custom blunder handler is very basic. We just make an exceptional work that can be considered when a blunder happens in PHP.

This work must most likely handle at least two parameters (blunder level and mistake message) yet can acknowledge up to five parameters (alternatively: record, line-number, and the blunder context):

Syntax

error_function(error_level,error_message,
error_file,error_line,error_context)
Parameter Description
error_level Required. Indicates the blunder report level for the client characterized blunder. Must be an esteem number. See table beneath for conceivable mistake report levels
error_message Required. Indicates the mistake message for the client characterized error
error_file Optional. Indicates the filename in which the blunder occurred
error_line Optional. Indicates the line number in which the mistake occurred
error_context Optional. Indicates an exhibit containing each factor, and their qualities, being used when the blunder occurred

Error Report levels

These mistake report levels are the distinctive sorts of blunder the client characterized blunder handler can be utilized for:

Value Constant Description
2 E_WARNING Non-deadly run-time blunders. Execution of the content isn't halted
8 E_NOTICE Run-time takes note. The content discovered something that may be a blunder, yet could likewise happen when running a content normally
256 E_USER_ERROR Fatal client created mistake. This resembles an E_ERROR set by the software engineer utilizing the PHP work trigger_error()
512 E_USER_WARNING Non-lethal client produced cautioning. This resembles an E_WARNING set by the developer utilizing the PHP work trigger_error()
1024 E_USER_NOTICE User-produced take note. This resembles an E_NOTICE set by the software engineer utilizing the PHP work trigger_error()
4096 E_RECOVERABLE_ERROR Catchable lethal mistake. This resembles an E_ERROR yet can be gotten by a client characterized handle (see additionally set_error_handler())
8191 E_ALL All blunders and alerts (E_STRICT turned into a piece of E_ALL in PHP 5.4)

Now lets make a capacity to deal with errors:

work customError($errno, $errstr) {
  echo "<b>Error:</b> [$errno] $errstr<br>";
  echo "Ending Script";
  die();
}

The code above is a basic mistake dealing with capacity. When it is activated, it gets the mistake level and a blunder message. It at that point yields the mistake level and message and ends the script.

Now that we have made a blunder taking care of capacity we have to choose when it ought to be triggered.


Set Error Handler

The default blunder handler for PHP is the worked in mistake handler. We are going to make the capacity over the default blunder handler for the term of the script.

It is conceivable to change the blunder handler to apply for just a few mistakes, that way the content can deal with various blunders in various ways. Be that as it may, in this precedent we are going to utilize our custom blunder handler for all errors:

set_error_handler("customError");

Since we need our custom capacity to deal with all mistakes, the set_error_handler() just required one parameter, a second parameter could be added to determine a blunder level.

Example

Testing the mistake handler by attempting to yield variable that does not exist:

<?php
/mistake handler function
work customError($errno, $errstr) {
  echo "<b>Error:</b> [$errno] $errstr";
}

/set mistake handler
set_error_handler("customError");

/trigger error
echo($test);
?>

The yield of the code above ought to be something like this:

Error: [8] Undefined variable: test

Trigger an Error

In a content where clients can include information it is valuable to trigger mistakes when an illicit info happens. In PHP, this is finished by the trigger_error() function.

Example

In this precedent a blunder happens if the "test" variable is greater than "1":

<?php
$test=2;
in the event that ($test>1) {
  trigger_error("Value must be 1 or below");
}
?>

The yield of the code above ought to be something like this:

Notice: Value must be 1 or below
in C:\myfolder\test.php on line 6

An mistake can be activated anyplace you wish in a content, and by including a second parameter, you can indicate what mistake level is triggered.

Possible mistake types:

  • E_USER_ERROR - Fatal client produced run-time blunder. Mistakes that can not be recouped from. Execution of the content is stopped
  • E_USER_WARNING - Non-lethal client created run-time cautioning. Execution of the content isn't ended
  • E_USER_NOTICE - Default. Client produced run-time take note. The content discovered something that may be a blunder, yet could likewise happen when running a content ordinarily

Example

In this model an E_USER_WARNING happens if the "test" variable is greater than "1". In the event that an E_USER_WARNING happens we will utilize our custom mistake handler and end the script:

<?php
/blunder handler function
work customError($errno, $errstr) {
  echo "<b>Error:</b> [$errno] $errstr<br>";
  echo "Ending Script";
  die();
}

/set blunder handler
set_error_handler("customError",E_USER_WARNING);

/trigger error
$test=2;
in the event that ($test>1) {
  trigger_error("Value must be 1 or below",E_USER_WARNING);
}
?>

The yield of the code above ought to be something like this:

Error: [512] Value must be 1 or below
Consummation Script

The yield of the code above ought to be something like this:

Error: [512] Value must be 1 or below
Website admin has been advised

And the mail got from the code above looks like this:

Blunder: [512] Value must be 1 or underneath

This ought not be utilized with all blunders. Normal blunders ought to be signed on the server utilizing the default PHP logging system.