Explanation of php error shielding error_reporting

Source: Internet
Author: User
Tags configuration settings php error reporting
In Windows: Why do multiple errors occur in a program originally running normally in php430 in 431? the general prompt is: Notice: Undefinedvarialbe: variable name. for example, the following code is displayed:

In Windows: for a program that runs normally in php4.3.0, why do multiple errors occur in php4.3.1? the general prompt is: Notice: Undefined varialbe: variable name. for example, the following code is displayed:

  1. If (! $ Tmp_ I ){
  2. $ Tmp_ I = 10;
  3. }

Run normally in 4.3.0. in 4.3.1, the prompt Notice: Undefined varialbe: tmp_ I will be displayed. The problem is as follows:

1. where is the problem?

2. how should I modify this code?

3. without changing the code, how can I modify the settings in php. ini to make the original program in 4.3.0 run normally in the 4.3.1 environment? This error message is not displayed.

Solution,Add error_reporting (E_ALL &~ E_NOTICE); or error_reporting (E_ALL ^ E_NOTICE );

Or modify php. ini:Error_reporting = E_ALL &~ E_NOTICE

Related error_reporting () functions:

Error_reporting () sets the PHP error reporting level and returns the current level. the error report is bit-by-bit, or the numbers are combined to get the expected error reporting 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 behaviors. (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

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). The effect is the same as that of error_reporting (E_ALL); // displays all errors.

A php Error was encountered, Severity: Notice, Message: 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.

Below are some information you have found:

Error_reporting () sets the PHP error level and returns the current level.

Syntax:Error_reporting (report_level)

If the level parameter is not specified, the current error level is returned. The following items are possible values of level:

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

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 the stat () function 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 representation ().

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.

PHP error report

The php. ini file contains many configuration settings. You should have set up your php. ini file and put it in the appropriate directory, as shown in instructions for installing PHP and Apache 2 on Linux. When debugging a PHP application, you should know two configuration variables. The two variables and their default values are as follows:

Display_errors = Off error_reporting = E_ALL

By searching for these variables in the php. ini file, you can find the current default values of these two variables. The purpose of the display_errors variable is obvious-it tells PHP whether an error is displayed. The default value is Off. However, to make the development process easier, set this value to On

Display_errors = On

The default value of error_reporting variable is E_ALL. This setting displays all information from poor coding practices to harmless prompts to errors. E_ALL is a little too detailed for the development process, because it displays a prompt on the screen for some trivial matters (for example, the variable is not initialized), it will mess up the browser output. I only want to see errors and bad code practices, but do not want to see harmless prompts. Therefore, replace error_reporting with the following default values: error_reporting = E_ALL &~ E_NOTICE

Restart Apache and set all the settings. Next, we will learn how to do the same thing on Apache.

Server error reports:

Depending on what Apache is doing, opening an error report in PHP may not work, because there may be multiple PHP versions on the computer. Sometimes it is difficult to tell which PHP version Apache is using, because Apache can only view one php. ini file. I don't know which php. ini file Apache is using to configure itself as a security issue. However, there is a way to configure the PHP variable in Apache to ensure that the correct error level is set.

In addition, it is better to know how to set these configuration variables on the server side to reject or preemptible the php. ini file to provide higher-level security.

When configuring Apache, you must have been exposed to the basic configuration in the http. conf file in/conf/httpd. conf.

To add the following lines to httpd. conf to overwrite any php. ini file:

Php_flag display_errors on php_value error_reporting 2039

This overwrites the flag set for display_errors and the value of error_reporting in the php. ini file. The value 2039 represents E_ALL &~ E_NOTICE. If you want to use E_ALL, set the value to 2047. Similarly, restart Apache.

Next, test the error report on the server.

The error_reporting () function can shield some error messages, but the errors caused by the PHP core cannot be blocked, errors caused by PHP core will directly cause PHP file compilation failure, because the writing format is not written in accordance with the PHP encoding rules, which cannot be blocked.

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.