Handling Errors In PHP
About the Author
Mike Rogers is a Web Developer who is currently at University studying Web Technologies. He also is the founder of Full On Design. He has several years experience freelancing and you can follow him on Twitter (Rogem002).
Handling errors in PHP can be quite a handful at times. Here is a really simple PHP class which I use to manage errors:
<?php
/*
errors class - Helps management of errors in a script.
@version
1.0
@author
Mike Rogers (FullOnDesin.co.uk)
@last updated
03 June 2009
@usage
You are free to share, modify and use this code for commercial or non-commercial uses.
Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.
You use this at your own risk.
*/
class errors {
var $errors_data;
/*
Add the error from $new_error into an array of errors.
@param
$new_error string The text related to your error.
@return:
True - Error has been Added
@example
add_error('Username is Incorrect');
*/
public function add_error($new_error){
$this->errors_data[] = $new_error;
return TRUE;
}
/*
Outputs the errors.
@param
None
@return:
- A div (ID - error) which contains the errors.
NULL - No errors
@example
echo output_errors();
*/
public function output_errors(){
if(is_array($this->errors_data)){
// Cycle through the errors.
foreach($this->errors_data as $error) {
$return .= '<p>'.$error.'</p>';
}
// Add it to the error div
return '<div id="error">'.$return.'</div>';
}
return NULL;
}
}
// @Example - creating the class:
$errors = new errors;
// @Example - Add an error
$errors->add_error('Username is incorrect');
// @Example - Return the errors
echo $errors->output_errors();
?>
If you are looking to fully integrate a script similar to the above, there is a really good post regarding the set_error_handler() function on Tinsology ( PHP Error Handling ).
[...] posted here: Handling Errors In PHP | Full On Design Tags: stumbleupon, news, [...]