Explanation of error_reporting () usage in PHP

Source: Internet
Author: User
Tags codeigniter
In php, we often use the error_reporting function to handle errors. We can see that the most common function is error_reporting (E_ALL ^ E_NOTICE). What does this mean? Let's take a look at it.

In php, we often use the error_reporting function to handle errors. We can see that the most common function is error_reporting (E_ALL ^ E_NOTICE). What does this mean? Let's take a look at it.

The error_reporting () function specifies which error to report. This function sets the error report level for the current script. This function returns the old error report level.

The error_reporting () function is used to set the error level and return the current level. It has 14 error levels, as shown below:

1 E_ERROR: Fatal runtime error. The error cannot be recovered. Script Execution is paused 2 E_WARNING non-fatal runtime error. Script Execution will not stop parsing errors during 4 E_PARSE compilation. Resolution errors should only be notified by the analyzer of the 8 E_NOTICE running time. 16 E_CORE_ERROR is a fatal error when PHP is started. This is like a non-fatal error of E_ERROR32 E_CORE_WARNING at PHP core startup. This is like a fatal compilation error with the PHP core E_WARNING warning 64 E_COMPILE_ERROR. This is like a non-fatal compile-time error generated by the Zend script engine. The Zend script engine generates an E_WARNING warning 256 E_USER_ERROR fatal user-generated error. 512 E_USER_WARNING non-fatal user-generated warning. 1024 E_USER_NOTICE user-generated notifications. 2048 E_STRICT running time notification. 4096 E_RECOVERABLE_ERROR capture fatal errors. 8191 E_ALL for all errors and warnings.

It seems that php is disabled by default, so you need to configure the php. ini file:

Change display_errors = Off TO display_errors = On.

In addition, the error level must be configured: Set

Change error_reporting = E_ALL:

Error_reporting = E_ALL &~ E_NOTICE

It should be because php displays all errors by default, and some harmless prompts do not need to be displayed, so set as above!

You can also use the following code in php:

<? Php // disable the error report, that is, the error error_reporting (0) is not displayed; // report the error error_reporting (E_ERROR | E_WARNING | E_PARSE) during running ); // report all errors: error_reporting (E_ALL);?>

Example:

I encountered a problem when I learned the CI framework today:

A php Error was encountered Severity: NoticeMessage: Undefined variable: user

Generally, if an undefined declared variable is output in the default PHP file, no error is reported, but an error is reported in the codeigniter framework, this is inconvenient for "lazy people" who want to integrate and add and modify pages. As a beginner, how can he block this error message in the code. I even used @, but many people have said that @ will greatly reduce the performance ....

Finally, I suddenly thought, was codeigniter intentionally prompted this error message? How should we block this type of error and accidentally found "How To Make codeigniter not display the Notice information ?", It turned out to be error_reporting (E_ALL) in index. php. You just need to change it
Error_reporting (E_ALL ^ E_NOTICE );
This error can be blocked without affecting other errors.

We may often see such a function in the program.

Function setErrorReporting () {// read from the configuration file whether it is the development environment if (DEV_ENV = true) {ini_set ("error_reprorting", "E_ALL &~ E_NOTICE "); ini_set (" display_errors "," on ");} else {error_reporting (E_ALL); ini_set ('display _ errors ', 'off '); ini_set ("log_errors", "On"); ini_set ('error _ log', '/var/log/phperror. log ');}}

Example:
In Windows: The program that was originally running normally in php4.3.0 has multiple errors in 4.3.1. The general prompt is: Notice: Undefined varialbe: variable name.

For example, the following code is available:
The Code is as follows:
If (! $ Tmp_ I ){
$ Tmp_ I = 10;
}
Run normally in 4.3.0. When running 4.3.1, the system prompts Notice: Undefined varialbe: tmp_ I.
The problem is as follows: 1. What is the problem?
2. How should I modify this code?
3. Do not change the code segment. How to modify the settings in php. ini so that the original program in 4.3.0 runs normally in the 4.3.1 environment without the error message.
Solution:

Add one sentence at the beginning of the program:
The Code is as follows:
Error_reporting (E_ALL &~ E_NOTICE); or error_reporting (E_ALL ^ E_NOTICE );
Or modify php. ini:
The Code is as follows:
Error_reporting = E_ALL &~ E_NOTICE
For the error_reporting () function: error_reporting (), set the PHP error level and return the current level.
; Error reports are reported by bit. Or add the numbers to get the expected error report level.
; E_ALL-all errors and warnings
; E_ERROR-fatal runtime error
; E_WARNING-runtime warning (non-fatal error)
; E_PARSE-parsing error during compilation
; E_NOTICE-runtime reminder (these are often caused by bugs in your code, or intentional behavior. (For example, an uninitialized variable is used based on the fact that the uninitialized variable is automatically initialized to an empty string)
; E_CORE_ERROR-fatal error during PHP startup Initialization
; E_CORE_WARNING-warning occurred during PHP startup initialization (non-fatal error)
; E_COMPILE_ERROR-fatal error during compilation
; E_COMPILE_WARNING-warning during compilation (non-fatal error)
; E_USER_ERROR-error message generated by the user
; E_USER_WARNING-user-generated warning message
; E_USER_NOTICE-user-generated reminder message
E_NOTICE indicates that the file is generally not recorded and used only when the program has an error, for example, an attempt to access a variable that does not exist or call stat () to view a file that does not exist.
E_WARNING is usually displayed, but the program execution will not be interrupted. This is effective for debugging. For example, call ereg () in a problematic General notation ().
E_ERROR is usually displayed and the program execution is interrupted. This mask cannot be used to trace memory configurations or other errors.
E_PARSE parses errors from the syntax.
E_CORE_ERROR is similar to E_ERROR, but does not include errors caused by the PHP core.
E_CORE_WARNING is similar to E_WARNING, but does not include PHP core error warnings.

Usage:
Error_reporting (0); // Disable Error Reporting
Error_reporting (E_ALL ^ E_NOTICE); // display all error messages except E_NOTICE
Error_reporting (E_ALL ^ E_WARNING ^ E_NOTICE); // display all error messages except E_WARNING E_NOTICE
Error_reporting (E_ERROR | E_WARNING | E_PARSE); // displays a runtime error, which is the same as error_reporting (E_ALL ^ E_NOTICE. Error_reporting (E_ALL); // display all errors
Error_reporting (0)
Error_reporting (255 );
Yes to list all prompts
Error_reporting (0 );
Yes. Do Not Display All prompts
Recommended
Error_reporting (7 );
Show only serious errors
1 E_ERROR fatal runtime error
2 E_WARNING runtime warning (non-fatal error)
4 E_PARSE parsing error during compilation
8 E_NOTICE runtime reminder (often a bug or intentional)
16 E_CORE_ERROR fatal error during PHP Initialization
32 E_CORE_WARNING warning during PHP initialization (non-fatal error)
64 E_COMPILE_ERROR fatal error during compilation
128 E_COMPILE_WARNING warning during compilation (non-fatal error)
256 user_error User-Defined fatal error
512 user_warning User-Defined warning (non-fatal error)
1024 User-Defined notification for E_USER_NOTICE (often bug or intentional)
2048 E_STRICT code standardization warning (how to modify to forward compatibility)
4096 E_RECOVERABLE_ERROR is close to a fatal runtime error. If it is not captured, it is treated as E_ERROR.
6143 all except E_STRICT errors (8191 in PHP6, that is, all errors)

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.