PHP error handling

PHP

PHP error handling methods:
  • Using the die() method
  • Custom error handling
Basic error handling: using the die() function Thedie() function prints a message and exits the current script.Syntax:
 die ($message)
Example :  
// PHP code showing default error handling $file = fopen ( "engineer.txt" , "w" ); ?>
Note.Run the above code and the engineer.txt file is missing, and then a runtime error will be displayed. 
Runtime Error:
PHP Warning: fopen (engineer.txt): failed to open stream: Permission denied in /home/dac923dff0a2558b37ba742613273073.php on line 2
To prevent this error, use the die() function. The die() function is implemented below:Example :  
// PHP code to check for errors 
// if there is no file
// then exit script if (! file_exists ( "engineer.txt" )) { die ( " File is not present " ); }  
// if file is present
// then continue else { $file = fopen ( " engineer.txt " , " w " ); }
?>
Note.If the file is engineer.txt is missing, it will display output. 
Exit
File is not present
Custom error handlers:Creating a custom error handler in PHP is very easy. Create a function that can be called when an error occurs in PHP.Syntax:
error_function ($error_level, $error_message, $error_file, $error_line, $error_context)
Parameters:This function takes five parameters as above and described below:
  • $error_level: This is a required parameter and must be an integer. There are predefined error levels.
  • $error_message:This is a required parameter and this is the message the user wants to print.
  • $error_file: is an optional parameter used to specify the file where the error occurred.
  • $error_line:is an optional parameter used to specify the line number where the error occurred.
  • $error_context:this is an optional parameter that is used to specify an array containing each variable and their value when an error occurs.
error_level :these are the possible error levels, which are listed below:
  • 1: .E_ERROR: fatal run-time error was stopped
  • 2: E_WARNING: non-fatal runtime error, script execution stopped
  • 4: E_PARSE: compile-time error generated by the parser
  • 8: E_NOT ICE: the script encountered something that could be an error
  • 16: E_CORE_ERROR: fatal errors that occurred during the initial run of the script
  • 32: E_CORE_WARNING: non-fatal errors that occurred during the initial script run
  • 8191: E_ALL: all errors and warnings
set_error_handler() function:After creating the myerror() function, you need to install a custom handler errors because PHP normally handles it, but if the user is doing custom error handling then the user should set it instead of an argument and pass the myerror function as a string.Example:  
// Create my error function that prints the message
// user function myerror ( $error_no , $error_msg ) { echo "Error: [$error_no] $error_msg" ; echo "Now Script will end " ; // If an error occurs, the script must be stopped die (); }  
// Setting set_error_handler set_error_handler ( "myerror" ); $a = 10; $b = 0;  
// This will generate an error echo ( $a / $b ) ;; ?>
Output:
Error: [2] Division by zero Now Script will end
Output:always try to handle errors using custom error handling because it will show a more specific message in according to the user, which can be useful to the user. If the error is not handled with custom error handling, then an error occurs, then the script will be stopped by default, but if it handles the error with custom error handling, it can continue the script after displaying the error message.



Get Solution for free from DataCamp guru