PHP Error Handling

Source: Internet
Author: User
Tags php error

Three methods of error handling:
Simple "die ()" statement
Custom errors and error triggers
Error Reporting

Basic error handling: Using the Die () function
The first example shows a simple script that opens a text file:

<? php$file=fopen ("welcome.txt","R");? >

If the file does not exist, you will get an error like this:
warning:fopen ( Welcome.txt) [function.fopen]: failed to open stream: no such file or directory in/w ww/test/test.php on line 2

<? PHP if (!file_exists ("welcome.txt")) Die    (" file does not exist " ); Else $file =fopen ("welcome.txt","R" );? >

Now, if the file does not exist, you will get an error message like this:
File does not exist

The above code is more efficient than the previous code because it takes a simple error-handling mechanism to terminate the script after the error.

Second, custom errors and error triggers

1. Create a custom error handler
Creating a custom error handler is straightforward. We have simply created a special function that can be called when an error occurs in PHP.
The function must be capable of handling at least two parameters (Error level and error message), but can accept up to five parameters (optional: File, Line-number, and error context):
Grammar:
error_function (error_level,error_message,error_file,error_line,error_context)

Parameters Description
Error_level Necessary. Specifies "Error reporting level" for user-defined errors. Must be a number. See the table below: Error reporting levels.
Error_message Necessary. Specifies error messages for user-defined errors.
Error_file Optional. Specifies the name of the file where the error occurred.
Error_line Optional. Specifies the line number where the error occurred.
Error_context Optional. Specifies an array that contains each variable that was used when the error occurred and their value.

Error Reporting level
These error reporting levels are different types of errors that are handled by user-defined error handlers:

value Constants Description
2 E_warning A non-fatal run-time error. Script execution is not paused.
8 E_notice Run-time notice. Occurs when a script discovers a possible error, but it may also occur when the script is running correctly.
256 E_user_error A fatal user-generated error. This is similar to the e_error that programmers use to set the PHP function Trigger_error ().
512 E_user_warning A non-fatal user-generated warning. This is similar to the e_warning that programmers use to set the PHP function Trigger_error ().
1024 E_user_notice User-generated notifications. This is similar to the e_notice that programmers use to set the PHP function Trigger_error ().
4096 E_recoverable_error A fatal error that can be caught. Similar to E_error, but can be captured by user-defined handlers. (see Set_error_handler ())
8191 E_all All errors and warnings. (In PHP 5.4, E_strict becomes part of E_all)

2. Setting up error handlers
The default error handler for PHP is the built-in error handler. You can modify the error handler so that it applies only to certain errors, so that the script can handle different errors in different ways.
Set_error_handler ("Customerror");
Since we want our custom function to handle all errors, Set_error_handler () requires only one parameter, and a second parameter can be added to define the error level.

3. Triggering errors
Where the user enters data in the script, it is useful to trigger an error when the user's input is invalid. In PHP, this task is done by the Trigger_error () function.
You can trigger an error anywhere in the script, and by adding a second parameter, you can specify the level of error that is triggered.
Possible types of errors:
E_user_error-Fatal user generated run-time error. The error cannot be recovered. The execution of the script was interrupted.
E_user_warning-A non-fatal user-generated run-time warning. Script execution is not interrupted.
E_user_notice-Default. User-generated Run-time notifications. Occurs when a script discovers a possible error, but it may also occur when the script is running correctly.

Instance
In this example, if the "test" variable is greater than "1", a e_user_warning error occurs. If e_user_warning occurs, we will use our custom error handler and end the script:

<?PHP//Error Handling functionsfunction Customerror ($errno, $errstr) {echo"<b>Error:</b> [$errno] $errstr <br>"; Echo"End of script"; Die ();}//setting error handling functionsSet_error_handler ("Customerror", e_user_warning);//Trigger Error$test =2;if($test >1) {Trigger_error ("variable value must be less than or equal to 1", e_user_warning);}?>

The output of the above code is as follows:
Error: [512] variable value must be less than or equal to 1
End of script

Third, Error Record
By default, PHP sends an error record to the server's record system or file, based on the Error_log configuration in php.ini. By using the Error_log () function, you can send an error record to a specified file or remote destination.
Sending an error message to yourself via e-mail is a good way to get notification of a specified error.

Send error message via e-mail
In the following example, if a specific error occurs, we will send an e-mail with an error message and end the script:

<?PHP//Error Handling functionsfunction Customerror ($errno, $errstr) {echo"<b>Error:</b> [$errno] $errstr <br>"; Echo"site administrator has been notified"; Error_log ("Error: [$errno] $errstr",1,"[email protected]","From : [email protected]");}//setting error handling functionsSet_error_handler ("Customerror", e_user_warning);//Trigger Error$test =2;if($test >1) {Trigger_error ("variable value must be less than or equal to 1", e_user_warning);}?>

The output of the above code is as follows:
Error: [512] variable value must be less than or equal to 1
Site administrator has been notified

Messages received from the above code are as follows:
Error: [512] variable value must be less than or equal to 1

This method is not suitable for all errors. General errors should be logged on the server by using the default PHP record system.

PHP Error Handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.