When you write a code or if you are running a script, errors will happen, how you handle these errors will make your code well written or badly coded. In this PHP Training course we will look at how to prevent or minimise the amount of errors you could have in a piece of code and also how to handle the errors if and when they occur. Lets begin:

To minimise the error in your code

  1. I would suggest you to use a tool for codding that will check the syntax of the PHP code for you or use the PHP manual constantly for spelling of different functions and statements in PHP. This will minimise the amount of syntax and also spell errors.
  2. Try to avoid using shorthand codes in PHP so instead of < ? ?> use < ?php ?>. This will help if your script is going to run on a server that doesn’t allow shorthand and you don’t have access to change it.
  3. Never assume that the coder will be entering all the right values always check and double check the values(Just don’t over do it).
  4. To test your code always try the border values for Example:

< ?php

$a=$_GET["value"];

if ($a>=4){echo "It is equal or bigger than 4";}

else{echo "It is less than 4";}

?>

Now to test the above code using the form that has a field called value use 3~ for less than 4, 4 and 5 ~ for equal and bigger than 4 to test the code

Now for error handling :

for basic error handling you can use function die(); to end the script and to tell the client what is wrong like:

< ?php

$a=2;

if($a==3){die(“This is the wrong number”);}

echo “This is another right Number”;

?>

The above Script will show : This is the wrong number and then the script will stop and will end.

You can also do custom error handling, the way that we format an error handling function in PHP is:

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

error level can be one of the below values:

Number Value PHP Constant used Description
2 E_WARNING Non-fatal run-time errors. Execution of the script will continue after giving the warning
8 E_NOTICE Run-time notices. PHP is not sure about something the code. It might be an error or it might not be and the script will continue running
256 E_USER_ERROR Fatal user-generated error. This is an error that a programmer will trigger using  trigger_error function and script will end.
512 E_USER_WARNING Non-fatal user-generated warning. This is a programmer generated error triggered by trigger_error function and script will continue running.
1024 E_USER_NOTICE User-generated notice. This is an E_NOTICE that is generated by the programmer using trigger_error function and script will continue running.
4096 E_RECOVERABLE_ERROR Catchable fatal error. This is an error that can be handled by user defined function and is fatal meaning that it will end the Script.
8191 E_ALL All errors and warnings.

error_desc is the text describing the error that has occurred.

error_file and error_line is used to pass what file and in what line number has the error occurred in.

error_context will return all values that has been used when the error occurred as an array.

One last thing you must use die() function or the script will continue from the line after the one that caused the error.

So to create a custom error handler function You can do something like:

function MyErrorHandler($level,$desc){

echo "".$level." :".$desc;

die;

}

This will show description of the error and then end the script.

Now we have to tell PHP to instead of using the internal error handler use ours for doing this use the following:

set_error_handler(error_handler, error_type);

So in this case the statement would be :

set_error_handler(“MyErrorHandler”);

There are times in a script that you need or want to trigger an error for the user for example if after verifying the form you see a value is wrong you can trigger an error using trigger_error() function:

trigger_error ( $error_msg ,$error_type );

$error_msg is the message for this error.

$error_type is the error type (error level).

example:

trigger_error(“This is a Test Error”);

You can use the custom function to do pretty much anything you want for example you could set it up to log the error in a file, send you an email, do some file management tasks or even trigger another error(Basically what PHP says is “Your Wish is My Command master!”).

%d bloggers like this: