Error message in PHP

Source: Internet
Author: User
From: Dao ke Yu Peng

Configure error messages in PHP. ini
In PhP4, there is no exception concept, only error. We can modify the php. ini file to configure the error message output by the user end.
In PHP. ini, a semicolon represents a comment.
PHP. ini divides the error types that can be displayed into the following types.
; E_all-all errors and warnings (excluding e_strict ).
; E_error-fatal runtime error
; E_recoverable_error-catch/handle errors that can be caught by the Exception Handling Mechanism
; E_warning-warning during running (non-fatal error)
; E_parse-parsing error during compilation
; E_notice-runtime prompts. These prompts are often caused by bugs in the Code and may be intentional (for example, using an uninitialized variable, in fact, it is automatically initialized into an empty string ).
; E_strict-indicates the runtime prompt that you can give PHP suggestions to change your code to achieve the best coordination and improve code compatibility.
; E_core_error-fatal error during PHP initialization startup.
; E_core_warning-PHP non-fatal error during initialization startup.
; E_compile_error-fatal compilation error.
; E_compile_warning-compilation error (non-fatal error ).
; E_user_error-user error information.
; E_user_warning-user warning information.
; E_user_notice-user prompt information.

In PHP. ini, error_reporting controls the types of messages that are output to the client.
The following configurations are recommended in PHP. ini.
Error_reporting = e_all indicates that all information is output.
Error_reporting = e_all &~ E_notice indicates that all errors are output, except the prompt.
Error_reporting = e_compile_error | e_recoverable_error | e_error | e_core_error indicates that all error information is output.
In PHP. ini, display_errors can be used to set whether to output the preceding error information to the user end.
Display_errors = on output to the client (this is more convenient when debugging code)

Display_errors = off the message will not be output to the user end (remember to change it to off when it is finally published to the user)
Handle error messages in PHP
In PHP, error handling is very loose. The PHP system tries its best to keep the program running unless it encounters a fatal error.
Example 5-2-1.php
Code snippet
Example: 5-2-1.php
<? PHP
Echo "start... <br/> ";
Echo $ t; echo "Run... <br/> ";
Echo $ B = 100/0; echo "Run... <br/> ";
$ A = new A (); echo "End ...... <br/> ";
Execution result
Example: 5-2-1.php
Start ......
Run... warning: Division by zero in 5-2-1.php on line 6
Run... fatal error: class 'A' not found in 5-2-1.php on line 8
Line 3: When printing an unassigned variable $ t, the system returns a notice with no defined variable.
When dividing 7th rows by 0, the system reports a warning like dividing by 0. The program is still running.
Row 3: When a nonexistent class is instantiated, a fatal error occurs and the program stops running.
Prompt again: if you do not want to display the error message to the user, set PHP. ini
Display_errors = off
Adjust the error level in PHP code
In addition to adjusting the display level of the error message in the PHP. ini file, you can also customize the display level of the message in the PHP code.

PHP provides a convenient adjustment function.
Int error_reporting ([int level])
This function can be used to define the display level of error messages on the current PHP page. The parameter level uses a binary mask combination.
1 e_error
2 e_warning
4 e_parse
8 e_notice
16 e_core_error
32 e_core_warning
64 e_compile_error
128 e_compile_warning
256 e_user_error
512 e_user_warning
1024 e_user_notice
2047 e_all
2048 e_strict
4096 e_recoverable_error
Example 5-2-2.php
Code snippet
Example: 5-2-2.php <br/>
<? PHP
Error_reporting (0 );
// Disable all error display
// Error_reporting (e_error | e_warning | e_parse );
// Error_reporting (e_error | e_warning | e_parse | e_notice );
// Error_reporting (e_all ^ e_notice); // PHP. ini error value.
// Error_reporting (e_all); // display all error messages.
// Ini_set ('error _ report', e_all );
// Code with the same effect as error_reporting (e_all.
Echo "start... <br/> ";
Echo $ t; echo "Run... <br/> ";
Echo $ B = 100/0; echo "Run... <br/> ";
$ A = new ();
Echo "End ...... <br/>";?>
Execution result
Example: 5-2-2.php
Start ......
Run ......
Run ......
Custom error handling
In PHP, you can customize the error handling method. First, you need to customize an error processing function, and then use the set_error_handler () function to declare the error processing function to the system. The error handler function is used for errors generated in the code.
Trigger_error () function can trigger an error. For example, trigger_error ("age you input must> = 0", e_user_error) triggers your own error information.
Example: 5-2-3.php
Code snippet
Example: 5-2-3.php
<? PHP
Function myerrorhandler ($ errno, $ errstr, $ errfile, $ errline ){
Switch ($ errno ){
Case e_user_error:
Echo "<B> my error </B> [$ errno] $ errstr <br/> \ n ";
Echo "Fatal error in line $ errline of File $ errfile ";
Echo ", PHP". php_version. "(". php_ OS. ") <br/> \ n ";
Echo "aborting... <br/> \ n ";
Exit (1 );
Case e_user_warning:
Echo "<B> my warning </B> [$ errno] $ errstr <br/> \ n ";
Case e_user_notice:
Echo "<B> my notice </B> [$ errno] $ errstr <br/> \ n ";
Default: Echo "Unknown error type: [$ errno] $ errstr <br/> \ n ";
Set_error_handler ("myerrorhandler"); // defines the error handling function. $ age =-100;
// Assume that a negative age occurs, which is obviously incorrect.
If ($ age <0 ){
Trigger_error ("age you input must> = 0", e_user_error );
// Trigger error ;}
Execution result
Example: 5-2-3.php
My error [256] age you input must> = 0 fatal error in line 27 of file D5-2-3.php, PHP 5.1.1 (winnt) aborting...

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: 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.