Post: [TUTORIAL] Using PHP Error Handler [TUTORIAL]
05-10-2011, 12:37 AM #1
(adsbygoogle = window.adsbygoogle || []).push({});
Originally posted by 039

[align=center]Hello guys. I see everyday php codes without error handlers showing the default errors so I decided to make a tutorial for error handlers..[/align]

[size=large][align=center][b][i]Examples of default errors above[/i][/b][/align][/size]

[align=center]
    Notice: Undefined variable: example in Sad Awesome\xampp\htdocs\example.php on line 19
[/align]
[align=center]
    Parse error: syntax error, unexpected '<' in Sad Awesome\xampp\htdocs\example.php on line 22
[/align]

[size=xx-large][i][align=center]Making a custom error handler[/align][/i][/size]

[size=large][b][align=center]Try running this code below[/align][/b][/size]
    <?php

echo "$example"; //$example variable doesn't exist so we get error!

?>

[size=large][b][align=center]You will get this error[/align][/b][/size]
    Notice: Undefined variable: example in Sad Awesome\xampp\htdocs\example.php on line 8


[align=center]Hmm,If you had a serious public script running would you want your visitors to get this error or a another default PHP error?[/align]
[align=center][size=large][i]I don't think so..Let's prevent it![/i][/size][/align]

[size=large][b][align=center]Add to the previous script the code below(This code should be in the beginning of the script)[/align][/b][/size]
    function Error() //We create a function named "Error"
{
die("Variable cannot be found<br>"); //Script ends and displays the message "Variable cannot be found"
}
set_error_handler("Error"); //We set our function as error handler!


[align=center][size=large]So we should have this code now[/size][/align]
    <?php

function Error() //We create a function named "Error"
{
die("Variable cannot be found<br>"); //Script ends and displays the message "Variable cannot be found"
}
set_error_handler("Error");//We set our function as error handler!

echo "$example"; //$example variable doesn't exist so we get error!

?>

[align=center][size=large]Now you should take this error[/size][/align]
    Variable cannot be found


[align=center][size=large]Congratulations[/size]
You made your first basic error handler![/align]

[align=center]
I will improve this guide,it's just late ^^
[/align]


source You must login or register to view this content.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo