We can handle default error in PHP in a very simple way. This can be done by sending an error message with its filename, error line number and a message that describes the error to the browser.
Error handling plays an important role whenever we make any php script or any php web application.,
Below you can check some error checking methods in PHP -->
- Simple "die()" statements. Custom
- errors and error triggers. Error
- reporting.
1- Using the die() function
<?php
if(!file_exists("hello.txt")) {
die("File is not found");
} else {
$file=fopen("welcome.txt","r");
}
?>
2- Custom Error Handler
We can create a custom error handlerfor handling the error. We simply create a function that can be called whenever an error occurs.
error_function(error_level,error_message,
error_file,error_line,error_context)
3-Error Report
These error report are having user-defined error handler that can be used for default error handing in PHP:
function customError($errno, $errstr) {
echo "<b>Error:</b> [$errno] $errstr<br>";
echo "Ending Script";
die();
}
0 Comment(s)