Error in PHP

Source: Internet
Author: User
Tags execution include ini variables php file php and php error variable
A problem is encountered today in learning the CI framework:

A PHP Error was encountered
Severity:notice

message:undefined Variable:user

Generally in the default normal php file output an undefined declaration of a variable is not reported wrong, but in the CodeIgniter framework to report errors, which want to integrate add and modify the page in one of the "lazy person" is not convenient, Because it's the beginner who wants to block this error hint in the code. Even the @, but listen to a lot of people say @ will greatly reduce performance ....

Finally suddenly thought, is not codeigniter intentionally let this error message hint out, how should we go to block out this kind of mistake? Accidentally search "How to let CodeIgniter not show notice information?", An epiphany. Error_reporting (E_all) in the entrance index.php; just change it to
Error_reporting (e_all ^ e_notice);
You can screen out this error without affecting other errors.

Below are some of the data that was searched:

Error_reporting () Sets the error level for PHP and returns the current level.

Grammar
Error_reporting (Report_level)
If the parameter level is not specified, the current error levels are returned. The following are the possible values for the level:

1 E_error
2 e_warning
4 E_parse
8 E_notice
E_core_error
E_core_warning
E_compile_error
128 E_compile_warning
256 E_user_error
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 used only if the program has an error condition, such as attempting to access a nonexistent variable, or calling a stat () function to view a file that does not exist.

E_warning are usually displayed but do not interrupt the execution of the program. This is very effective for debugging. For example: Call Ereg () with the general representation of the problem.

E_error are usually displayed and interrupt program execution. This mask will not be able to trace the memory configuration or other errors.

E_parse parse 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 the PHP core error warning.

Error reporting for PHP
There are many configuration settings in the php.ini file. You should have set up your own php.ini file and put it in the appropriate directory, as shown in the documentation Instructions for PHP and Apache 2 on Linux. When debugging a PHP application, you should know two configuration variables. The following are the two variables and their default values:
Display_errors = Off
error_reporting = E_all
By searching for them in the php.ini file, you can find the current default values for these two variables. The purpose of the display_errors variable is obvious-it tells PHP if it shows an error. The default value is off. However, to make the development process easier, set this value to on:
Display_errors = On
The default value for the error_reporting variable is e_all. This setting displays all information from bad coding practices to harmless hints to errors. E_all is a bit too thin for the development process because it displays hints on the screen for trivial things, such as variable initialization, and can mess up the browser's output. I just want to see errors and bad coding practices, but don't want to see harmless hints. Therefore, replace the default value of error_reporting with the following values:
error_reporting = E_all & ~e_notice

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

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

Also, it's best to know how to set these configuration variables on the server side to veto or preempt php.ini files, providing a higher level of security.
When you configure Apache, you should have contacted the basic configuration in the http.conf file in/conf/httpd.conf.

To do what you've done in the php.ini file, add the following lines to httpd.conf, overwriting any php.ini files:
Php_flag display_errors on
Php_value error_reporting 2039
This overrides the flag already set for display_errors in the php.ini file, and the error_reporting value. The value 2039 represents E_all & ~e_notice. If you prefer to use E_all, set the value to 2047. Again, restart Apache.
Next, you will test the error report on the server.

About the error_reporting () This function, it can be masked to some error messages, but the PHP core caused by the error, is not able to block, because the PHP core caused by the error will directly lead to PHP file compilation failure, because the writing format is not in accordance with the coding rules of PHP errors caused by , it's not shielded.
Copy CodeThe code is as follows:
* For now, avoid warnings of e_strict mode
* (This must is 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);

The following are common:
Copy CodeThe code is as follows:
Turn off all error reporting;
error_reporting (0);

The report simply running errors; reports a simple run error
Error_reporting (E_error e_warning e_parse);

Reporting E_notice can be good too (to) uninitialized
Variables or catch variable name misspellings ...); Includes a report of some uninitialized variables or a spelling error catching variable names
Error_reporting (e_error e_warning e_parse e_notice);

All errors except E_notice
This is the default value set in PHP.ini, all errors are reported but not e_notice that is also php.ini defaults
Error_reporting (e_all ^ e_notice);

Report All PHP errors (bitwise/May is used in PHP 3); reports all errors
Error_reporting (E_all);

Same as error_reporting (E_all);
Ini_set (' error_reporting ', e_all);


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.