Error Handler Function

Written by @m_k_amin 22 December 2012

It is not a professional work that your page failed because of unpredicted errors. You should handle all possible cases. This script is a function of handling errors. We have made a mathematical example for this function called 'Factorial'. When the input is not in correct format, the script trigger an error manually to the user and jumps from failure. You can manage all error types by this function, except some critical ones which PHP doesn't allow to handle them manually.

Code Snippet:

                                                
                                                <!-- this script is provided by www.phpfreecode.com coded by: Kerixa Inc. -->
<!-- This Script is from www.hawkee.com, found at www.phpfreecode.com-->
<form method="POST" action="">
          <p style="text-align: center">
		  <input type="text" name="TF" size="20" value="<?php if (isset($_POST['FC']))echo $_POST['TF'] ?>">&nbsp;
          <input type="submit" value="Factorial !" name="FC"></p>
</form>
<?php
function factorial ($num){
	
	if (!(is_numeric($num)))
		trigger_error("Non numeric input!",E_USER_ERROR);
                  
    if ($num<0)
        trigger_error("Nagative Number!",E_USER_ERROR);
 
    if ($num>= pow(10,6))
        trigger_error('Very Large Inputted Number may lead to delay on calculating result!',E_USER_WARNING);
                                               
    $result= 1;
    for ($i=2; $i<=$num; $i++)
        $result=$result*$i;
                  
    if (is_infinite($result))
        trigger_error("Large Inputted Number leads to infinit result!",E_USER_NOTICE);
                  
    return $result;
}
 
function errHandler ($errNum, $errStr, $errFile, $errLine){
	$errFile= basename($errFile);
	switch ($errNum){
		case E_USER_ERROR:
			//echo "Fatal Error $errNum) <b>$errStr<br>$errFile</b> is stopped on line <u>$errLine</u>.";
			$kind='Fatal';
			$txt='is stopped';
			$ex=true;
			break;
			
		case E_ERROR:
			//echo "Fatal Error $errNum) <b>$errStr<br>$errFile</b> is stopped on line <u>$errLine</u>.";
			$kind='Fatal';
			$txt='is stopped';
			$ex=true;
			break;
			
        case E_USER_WARNING:
            //echo "Warning $errNum) <b>$errStr<br></b>Reprted from <b>$errFile</b> on line <u>$errLine</u> but the script continued.<br>";
            $kind='Warning';
            $txt='has error';
            break;
            
        case E_WARNING:
            //echo "Warning $errNum) <b>$errStr<br></b>Reprted from <b>$errFile</b> on line <u>$errLine</u> but the script continued.<br>";
            $kind='Warning';
            $txt='has error';
            break;

        case E_USER_NOTICE:
            //echo "Notice $errNum) <b>$errStr<br></b>Reprted from <b>$errFile</b> on line <u>$errLine</u> but the script continued.<br>";
            $kind='Notice';
            $txt='has error';
            break;
        
        case E_NOTICE:
            //echo "Notice $errNum) <b>$errStr<br></b>Reprted from <b>$errFile</b> on line <u>$errLine</u> but the script continued.<br>";
            $kind='Notice';
            $txt='has error';
            break;
    
		default:
			$kind='';
            $txt='has error';
			echo $errNum;
			break;
	}
	echo "
	<div style='text-align: center'>
	<span style='font-size: 14pt'><span style='color: #8D0FBC'><strong>$kind Error $errNum)</strong></span>
	<span style='color: #BC0F0F'>$errStr</span></span><b><br style='font-size: 14pt'></b>
	<span style='font-size: 14pt; color: #315D26'>$errFile $txt on line 
	</span> <u><span style='font-size: 14pt; color: #315D26'>$errLine</span></u>.</div>
	";
	if ($ex==true) die('<br><div style="text-align: center; height: 15px">
	<font face="Tahoma"><a target="_blank" href="http://www.phpfreecode.com/">
	<span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font></div>
	');
}
set_error_handler('errHandler');
if (isset($_POST['FC'])){
	$n= $_POST['TF'];
	echo "<p align='center'> $n! = ".factorial($n).'</p>';
}
?>
<br><div style="text-align: center; height: 15px">
	<font face="Tahoma"><a target="_blank" href="http://www.phpfreecode.com/">
	<span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font></div><a target='_blank' href='www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                            

Example:


About @m_k_amin

This user is pending a biography.

M

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2