We’ve all encountered errors, especially during the synthesis of a new code. This is what a typical error looks like when you run it at a server at home:

Parse error: parse error in C:\directory\10_3.php on line 4

Here, we know that the error was a parse error and that it happened in a file called 10_3.php and it was on line 4. A parse error is typically just a missing semi-colon or a bracket. Nothing major, but it still triggers a fatal error. You can read all about the different errors and how to deal with them in the PHP manual or at this nifty Zend tutorial. I learned most of my PHP skills from that tutorial so read through it because it will definitely help you out.

Now, if you have your own webpage set up, you certainly don’t want the users to see all that stuff, right? They won’t understand what it means and it’s not a nice way to show an error anyways.

There are  numerous ways to deal with error. You can use the try catch method to eliminate and deal with errors. I’m not fluent in that method so I won’t even try to explain it.  One way to deal with non-fatal errors is adding this to your PHP script:

error_reporting(E_ERROR);

This will only trigger errors that are fatal to the system. You can add “~” in front of the “E_ERROR” inside the parenthesis and PHP will not trigger any fatal errors but it will all the others. If you want extra errors to report, such as if a number is negative instead of positive, or whatever else. Use this piece of script:

function isnumbernegative ($num{

if ($num < 0 ) {

trigger_error (“Number is negative”, E_USER_ERROR);

}

}

This script will always trigger a user error. You can change the “E_USER_ERROR” to “E_USER_WARNING” which will let the script go on but will still display the error. Now let’s move on to something more complicated. I wrote a simple error-reporting script that will greatly help you out. It’s a simple text-based error-logging system using the function set_error_handler:

set_error_handler(‘error’);

function error($type, $msg, $file, $line, $context) {
echo “<h1>Error!</h1>”;

$date = date(‘l jS \of F Y h:i:s A’);

echo “Here is the information provided by the script:”;

echo “<hr><pre>”;
//shows the error code
echo “Error code: $type<br />”;

//error message
echo “Error message: $msg<br />”;

//error in what file and what line
echo “Script name and line number of error: $file:$line<br />”;
$variable_state = array_pop($context);
echo “Variable state when error occurred: “;
print_r($variable_state);
echo “</pre><hr><br>”;

//logging the error

$dir = ‘…/text_files/errors.txt ‘ or die (‘Could not be found’);
$open = fopen($dir,‘a’) or die (‘Could not be opened’);

fwrite($open, “ERROR: $date
type: $type,
msg: $msg,
file: $file,
line: $line,
context: $context

) or die (‘Could not be written’);

die (‘Error has been logged<br>’);

}

Next enclose the script in “<?php” and “?>” and save it. The original script that I based this one on did not have the text file logging capability. A few pointers: replace the “…/text_files/errors.txt” with a full directory name. You also have to create your own “errors.txt” file and if you don’t delete the entries at least once in a while, you’ll end up with a very large file over time. You can include a simple “if” loop and the function “file_exists()” to deal with the file creation. The next step is to implement the script into your website. You can use “include(‘script.php’)” or use “require(‘script.php’)” any one of these. I suggest you make a separate folder for scripts. The error will show up like this:

Error!

Here is the information provided by the script:


Error code: 2
Error message: Wrong parameter count for explode()
Script name and line number of error: C:\directory\12.php:17
Variable state when error occurred: string

The errors.txt will apear slightly differently but will include the same data plus time and date. Anyways, that’s it. Thank you for reading, and I hope you enjoyed this rather simple tutorial on error reporting.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Reddit
  • Tumblr
  • Twitter
  • email
  • Slashdot
  • StumbleUpon