Saturday, 31 January 2015

Error Handling

Errors handling can be done by using set_error_handler() method we can display custom errors by using this method eg:


<?php

//  custom error handler
set_error_handler('custom_error');

// this will genrate notice
echo $myvariable;
// custom error handler
function custom_error($type, $msg, $file, $line, $context) {
    switch ($type) {
        // notices
        case E_NOTICE:
           print "Notice Custom Message on  line $line of  $file: $msg <br />"; 
            break;
        
        // warnings
        case E_WARNING:
            // report error
            print "Non-fatal error Custom Message on line $line of $file: $msg <br />";
            break;

        // other or fatal 
        default:
            print "Fatal Error Custom Message  of type  $type on line  $line of $file: $msg <br />"; 
            break;
    }
}

?>

No comments: