Learn how to use error_reporting

Source: Internet
Author: User
Tags configuration settings
Learn how to use error_reporting

The usage frequency of error_reporting in php programming is clear to everyone. This article introduces the usage of error_reporting for your reference. If the level parameter is not specified, the current error level is returned. The following items are the possible values of level: 1 E_ERROR2 E_WARNING4 E_PARSE8 E_NOTICE16 E_CORE_ERROR32 paie_compile_error128 contains invalid bytes E_ALL2048 records, which indicates that the general situation is not recorded and is used only when the program has an error, for example, an attempt to access a non-existent variable or call the stat () function to view a non-existent file. 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.

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 = Offerror_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 depend on what Apache is doing. opening error reports 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.

In php. add the following lines to httpd. conf, overwrite any php. ini file: php_flag display_errors onphp_value error_reporting 2039 this will overwrite in php. in the ini file, set the flag display_errors and the value of error_reporting. 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 PHP encoding rules, which cannot be blocked.

  1. * For now, avoid warnings of E_STRICT mode
  2. * (This must be done before function definitions)
  3. */
  4. If (defined ('e _ STRICT ')){
  5. $ Old_error_reporting = error_reporting (0 );
  6. If ($ old_error_reporting & E_STRICT ){
  7. Error_reporting ($ old_error_reporting ^ E_STRICT );
  8. } Else {
  9. Error_reporting ($ old_error_reporting );
  10. }
  11. Unset ($ old_error_reporting );

The following are common examples:

  1. // Turn off all error reporting; disable all errors
  2. Error_reporting (0 );
  3. // Report simple running errors; Report a simple running error
  4. Error_reporting (E_ERROR | E_WARNING | E_PARSE );
  5. // Reporting E_NOTICE can be good too (to report uninitialized
  6. // Variables or catch variable name misspellings ...); Including reporting uninitialized variables or spelling errors for capturing variable names
  7. Error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE );
  8. // Report all errors before T E_NOTICE
  9. // This is the default value set in php. ini; reports all errors but does not include E_NOTICE
  10. Error_reporting (E_ALL ^ E_NOTICE );
  11. // Report all PHP errors (bitwise 63 may be used in PHP 3); Report all errors
  12. Error_reporting (E_ALL );
  13. // Same as error_reporting (E_ALL); Same as above
  14. Ini_set ('error _ report', 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.