PHP Tutorials

How to Get Error Message in PHP?

Getting errors on web pages is not out of the ordinary. However, it is important to find the best ways of dealing with these errors. PHP offer simple and effective error handling techniques. Simply put, errors are handled with error messages, which contain the name of the file, line number and the details describing the sent error.

The essence of error handling in PHP

Creation of web applications and scripts is a serious matter. This is especially in terms of security and handling errors. Codes that lack error-checking functionalities look unprofessional and are vulnerable to security compromise. The below article seeks to look deeper into ways of getting and handling error messages on PHP. It will highlight the following methods:

  • Using “die()” function
  • Specific errors and how to trigger those errors
  • Ways of reporting errors

Critical error handling by the use of die() utility

This following is an example of a script, which is used to open a txt file:

$file=fopen)”see you.txt”,”r”);

However, when the file is nonexistent, users will see an error similar to this one:

Warning: fopen(see you.txt)  [function.fopen]:  fails to open the stream:

Invalid directory address in G:\webfolder\test.php on line 2

It is important for the programmer to test for errors. It is a good and safe way to stop getting such errors on the clients or users end. The following is a simple technique of testing whether the file exists.

if(!file_exists(“see you.txt”)) {
did(“File not Found”);
}  else {
$file=fopen(“see you.txt”,”r”);

However, there are times a file may not exist. The following is the error users will see:

File not found

This second code is more professional than the first one. This is because it utilizes an error handling system, which is simple but effective to discontinue the error. On the other hand, the solution is not always to stop the script. The following are some alternatives to handling errors.

Creation of custom error handlers

Custom error handling creation is simple. It only takes creation of a special function, which is called whenever an error occurs. It is compulsory for the function to work with at least two parameters (message error and error level). However, it can accept a maximum of five parameters. The error handler syntax looks like this:

error_function (where you put the names like error_level, for levels, message, files, lines, context etc.)

The following are the parameters in which this function works.

  • Error_level: Required. It defines the level of the report of the error and defines the user error
  • Error_message: Required. It outlines the message of the error of the error from the user
  • File_error: this is selective, as it gives the specific name of the file from the location of the error
  • Error_line: this is discretionary and It gives the specific line number where the error happened
  • Error_context: optional. It displays the arrays that contain the variables and values of the occurrence of the error

There are various error report levels. These depend on the severity and seriousness. The following is an outline of the levels of error reports.

  • 2: E_WARNING: this is not a fatal run time error and script execution is halted
  • 8: E_NOTICE: these are run-time notifications. What this means is that the script identifies a potential error, but the script can run usually
  • 256: E_USER_ERROR: this is not a fatal warning, it is similar to the E_WARNING, which is programmer set
  • 512: E_USER_WARNING: this is a notice generated by the user and it is similar to the E_WARNING, which is programmer set, with the use if the trigger_error() function
  • 1024: E_USER_NOTICE: this is a user contributed notice and it is similar to the E_NOTICE, which is programmer set
  • 4096: E_RECOVERABLE_ERROR: this is a fatal error, which can be captured. It is captured by a handle defined by the user
  • 8191: E_ALL: this is a collection of all the warnings and errors

This code is a simple, effective, and effortless function for handling errors. When it is active, it acquires the level of the error and message. The next step is to terminating the script, but before that, it determines the error level and sends the message.

Error logging

This is a very important step for effective management of errors and logs. It is a default feature of PHP to send a log of errors to the logging system of the server or file. However, this is dependent on how the error_log configuration. The error_log() is perhaps the most effective way of sending logs of errors to specific files or a distant destination.

The following is a vivid illustration of sending error messages via e-mail. It is a fast, efficient and effective way of logging errors.

//error handling function
Function customError)$errno,$errstr) {
echo “<b>Error:</b>  [$errno]  $errstr<br>”;
echo  “Mail handler is informed”;
error_log(“Error:  [$errno]  $errstr”,1,
[email protected],”From: [email protected]”);
//triggering error
If  ($test>3) {
Trigger_error(“Value must be lower or 3”, E)USER_WARNING);

The output code from the above procedure will resemble this:

Error: [512] the value should be 3 or less

Webmaster is now informed

The mail from submitting the code will look this way:

Error: [512] the value should be 3 or less

This is an effective way of logging errors.

Leave a Reply

Your email address will not be published. Required fields are marked *