Usage of error_reporting() function in PHP (modify PHP shielding errors)_PHP tutorial

WBOY
Release: 2016-07-21 15:27:46
Original
931 people have browsed it

I encountered a problem while learning the CI framework today:

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: user

Usually in the default Outputting an undefined declared variable in an ordinary PHP file will not report an error, but under the codeigniter framework, an error will be reported. This is very inconvenient for "lazy people" who want to integrate adding and modifying pages, because it is the first time. At first, scholars thought about how to block this error message in the code. They even used @, but many people said that @ would greatly reduce performance...

Finally, it suddenly occurred to me whether codeigniter intentionally caused this error. The information prompt came out. How should we block this type of error? I accidentally searched for "How to prevent codeigniter from displaying Notice information?", and I suddenly had an idea. It turned out that the error_reporting(E_ALL); in the entry index.php was causing trouble. Just change it to
error_reporting(E_ALL ^ ​​E_NOTICE);
to block this error without affecting other errors.

Here are some of the information found:

error_reporting() sets the error reporting level of PHP and returns the current level.

Syntax
error_reporting(report_level)
If the parameter level is not specified, the current error reporting level will be 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 general situation is not recorded, and is only used when the program has an error, such as trying to save Get 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 does not interrupt program execution. This is useful for debugging. For example: calling ereg() with the problematic regular notation.

E_ERROR is usually displayed and will interrupt program execution. This means that memory configuration or other errors cannot be traced using this mask.

E_PARSE Parses errors from the syntax.
E_CORE_ERROR Like E_ERROR, but excludes errors caused by the PHP core.
E_CORE_WARNING Like E_WARNING, but does not include PHP core error warnings.

PHP Error Reporting
There are many configuration settings in the php.ini file. You should have already set up your php.ini file and placed it in the appropriate directory, as shown in the documentation for installing PHP and Apache 2 on Linux. There are two configuration variables that you should be aware of when debugging PHP applications. Here are these two variables and their default values:
 display_errors = Off
 error_reporting = E_ALL
 The current default values ​​of these two variables can be found by searching for them in the php.ini file. The purpose of the display_errors variable is obvious - it tells PHP whether to display errors. The default value is Off. However, to make the development process easier, set this value to On:
 display_errors = On
 The default value of the error_reporting variable is E_ALL. This setting will show everything from bad coding practices to harmless tips to errors. E_ALL is a bit too granular for development purposes, as it also displays hints on the screen for small things (such as variables not being initialized), which messes up the browser's output. I only want to see errors and bad coding practices, but not harmless tips. So, please replace the default value of error_reporting with the following value:
 error_reporting = E_ALL & ~E_NOTICE

  Restart Apache and you are all set. Next, you'll learn how to do the same thing on Apache.

Error reporting on the server
Depending on what Apache is doing, turning on error reporting in PHP may not work because there may be multiple versions of PHP on the computer. Sometimes it's difficult to tell which PHP version Apache is using because Apache can only look at one php.ini file. Not knowing which php.ini file Apache is using to configure itself is a security issue. However, there is a way to configure PHP variables in Apache so that the correct error level is set.

Also, it is good to know how to set these configuration variables on the server side to override or preempt the php.ini file, thus providing a higher level of security.
When configuring Apache, you should have already been exposed to the basic configuration in the http.conf file in /conf/httpd.conf.

To do what you have already done in the php.ini file, add the following lines to httpd.conf, overriding any php.ini files:
 php_flag display_errors on
 php_value error_reporting 2039
 This will overwrite the flags already set for display_errors in the php.ini file, as well as the value of error_reporting. The value 2039 represents E_ALL & ~E_NOTICE. If you prefer to use E_ALL, set the value to 2047. Again, you still need to restart Apache.
Next, test error reporting on the server.

Regarding the error_reporting() function, it can shield some error messages, but errors caused by the PHP core cannot be shielded, because errors caused by the PHP core will directly cause the PHP file compilation to fail, because writing Errors caused by the format not being written in accordance with PHP coding rules cannot be blocked

Copy the code The code is as follows:

* For now, avoid warnings of E_STRICT mode
* (this must be done before function definitions)
*/
if (defined('E_STRICT')) {
$old_error_reporting = error_reporting(0);
if ($old_error_reporting & E_STRICT) {
error_reporting($old_error_reporting ^ E_STRICT);
} else {
error_reporting($old_error_reporting);
}
unset($old_error_reporting);

Common ones are as follows:
Copy code The code is as follows:

// Turn off all error reporting; Close all errors
error_reporting(0);

// Report simple running errors; Report a simple running error
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings …); including reporting some uninitialized variables or catching misspellings of variable names
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini; Report all errors except E_NOTICE This is also a shortcoming of php.ini Provincial settings
error_reporting(E_ALL ^ ​​E_NOTICE);

// Report all PHP errors (bitwise 63 may be used in PHP 3); Report all errors
error_reporting(E_ALL);

// Same as error_reporting(E_ALL); Same as above
ini_set('error_reporting', E_ALL);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323727.htmlTechArticleI encountered a problem while learning the CI framework today: A PHP Error was encountered Severity: Notice Message: Undefined variable: user usually outputs an undefined sound in the default ordinary PHP file...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!