Tutorial PHP Error handling

in #utopian-io7 years ago

Tutorial PHP Error handling

PHP Error Handling

In Some case application will not work exactly as we want and result will be shown an error. There are number of cases in which error may occur. Some cases are listed below.

⦁ Disk space might be full on web server.
⦁ Invalid value might be filled in form by user.
⦁ Sometime database or any file which user is trying to
access might be deleted or may not exist.
⦁ Permission for accessing some files for our application
might not given.
⦁ Sometimes services are down and when users are
trying to access that service, which are unavailable
than error occurs.

The above shown error are occurs when script runs and therefore it is known as runtime errors. These errors should be fixed before the script runs because those are different from the syntax errors.

An application should be built in such a way that it must have ability to control these type errors. In other words these means to make the user understand easily about errors and why they occurs.

Error Levels

More often than not, when there's an issue that keeps a content from running legitimately, the PHP motor triggers a mistake. Every mistake is spoken to by a whole number esteem and a related consistent. The accompanying table show a portion of the regular mistake levels.

1.png

For more error levels, please look at the reference on PHP Error Levels.

The PHP engine triggers an error at whatever point it experiences an issue with your script, however you can likewise trigger errors yourself to produce more easy to use error messages. Along these lines you can make your application more sofisticated. The accompanying area portrays some of regular techniques utilized for taking care of errors in PHP.

Error Handling using the die() function

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

If document does not exist you may get an error like this.

Warning: fopen(welcome.txt) [ function.fopen] : failed to open stream:
No such file or directory in D:\folder \test.php on line 1

To keep the client from getting an error message like the one above, we test whether the document exist before we attempt to get to it.

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

Presently if the record does not exist you get an error like this.

File not found

The code above is more proficient than the prior code, since it utilizes a straightforward error dealing with system to stop the script after the error.

In any case, basically ceasing the script isn't generally the correct approach. We should investigate elective PHP capacities for dealing with errors.

Custom Error Handler

Making a custom error handler is very straightforward. We basically make a unique capacity that can be called when an error happens in PHP.

This capacity must have the capacity to deal with at least two parameters (error level and error message) yet can acknowledge up to five parameters (optionally: file, line-number, and the error context).

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

lets makes function to handle errors.

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

The code above is a basic error taking care of capacity. When it is activated, it gets the error level and an error message. It at that point yields the error level and message and ends the script.

Since we have made an error taking care of capacity we have to choose when it ought to be activated.

Error Handler

The default error handler for PHP is the worked in error handler. We will make the capacity over the default error handler for the span of the script.

It is conceivable to change the error handler to apply for just a few errors, that way the script can deal with various errors in various ways. In any case, in this illustration we will utilize our custom error handler for all errors.

set_error_handler("customError");

Example

1

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

//set error handler
set_error_handler("customError");

//trigger error
echo($test);
? >

Output

2.png

2

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

Output

3.png

3

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

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

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

Output

4.png



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Ah very interesting, you know your shizzels Brother

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @brothermic I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.029
BTC 58157.69
ETH 3122.82
USDT 1.00
SBD 2.42