PHP error mechanism knowledge

Source: Internet
Author: User
Tags parse error
This article mainly introduces the relevant information about the PHP error mechanism knowledge. it is also very complicated for anyone who needs to refer to the PHP error mechanism. after several years of php, I have not summarized it carefully, make up this lesson now.

Note: The PHP version of this article uses 5.5.32.

PHP error level

First, you need to understand the php errors. As of php5.5, there were 16 error levels

Note: Make sure to enable error_log when trying the following code:

error_reporting(E_ALL);ini_set('display_errors', 'On'); 

E_ERROR

This is a Fatal Error, and the Fatal Error will be displayed on the page. when this Error occurs, the program will not be able to continue running.

Error example:

// Fatal error: Call to undefined function hpinfo() in /tmp/php/index.php on line 5hpinfo(); //E_ERROR 

Note: This level is also triggered if an uncaptured exception exists.

// Fatal error: Uncaught exception 'Exception' with message 'test exception' in /tmp/php/index.php:5 Stack trace: #0 {main} thrown in /tmp/php/index.php on line 5throw new \Exception("test exception"); 

E_WARNING

This error is only a Warning. the script will not be terminated, and the program will continue. the error message displayed is Warning. For example, include a non-existent file.

//Warning: include(a.php): failed to open stream: No such file or directory in /tmp/php/index.php on line 7//Warning: include(): Failed opening 'a.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /tmp/php/index.php on line 7include("a.php"); //E_WARNING 

E_NOTICE

This error is more mild, prompting you that this should not be written here. This is also a running error. the error code may be normal elsewhere, but it may occur in the current context.

For example, if the $ B variable does not exist, we assign it to another variable.

//Notice: Undefined variable: b in /tmp/php/index.php on line 9$a = $b; //E_NOTICE 

E_PARSE

This error occurs during compilation. syntax errors are found during compilation and cannot be analyzed.

For example, the following z is not set as a variable.

// Parse error: syntax error, unexpected '=' in /tmp/php/index.php on line 20z=1; // E_PARSE 

E_STRICT

This error was introduced after PHP5. your code can be run, but it is not recommended by PHP.

For example, in the function parameter transfer ++ symbol

// Strict Standards: Only variables should be passed by reference in /tmp/php/index.php on line 17function change (&$var) {$var += 10;}$var = 1;change(++$var);// E_STRICT 

E_RECOVERABLE_ERROR

This level is actually the ERROR level, but it is expected to be captured. if it is not captured by ERROR processing, the performance is the same as E_ERROR.

The type is often defined in the form parameter, but the error type is passed in during the call. Its error reminder is also preceded by a Catachable word than the fatal error of E_ERROR.

//Catchable fatal error: Argument 1 passed to testCall() must be an instance of A, instance of B given, called in /tmp/php/index.php on line 37 and defined in /tmp/php/index.php on line 33class A {}class B {}function testCall(A $a) {}$b = new B();testCall($b); 

E_DEPRECATED

This error indicates that you have used an old function version, which may be disabled or not maintained in later versions.

For example, the curl CURLOPT_POSTFIELDS uses \ @ FILENAME to upload files.

// Deprecated: curl_setopt(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead in /tmp/php/index.php on line 42$ch = curl_init("http://www.remotesite.com/upload.php");curl_setopt($ch, CURLOPT_POSTFIELDS, array('fileupload' => '@'. "test")); 

E_CORE_ERROR, E_CORE_WARNING

These two errors are generated by the PHP engine and occur during PHP initialization.

E_COMPILE_ERROR, E_COMPILE_WARNING

These two errors are generated by the PHP engine during compilation.

E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE, E_USER_DEPRECATED,

These errors are all made by the user. trigger_error is used, which is equivalent to a single port to send various error types to the user. This is a good way to escape try catch exceptions.

trigger_error("Cannot pide by zero", E_USER_ERROR);// E_USER_ERROR// E_USER_WARING// E_USER_NOTICE// E_USER_DEPRECATED 

E_ALL

All error and warning information of the E_STRICT field.

Error Control

Many configurations and parameters in php can control errors and display error logs. Step 1: What are the incorrect configurations in php?

According to the php + php-fpm model, there are actually two configuration files that will affect the php error display. one is the php configuration file. ini, another is the configuration file of php-fpm, php-fpm.conf.

Configuration in php. ini

Error_reporting = E_ALL // report error level, level of error_log =/tmp/php_errors.log // The log location display_errors = On // whether to display errors On the output, this output may be a page, or stdoutdisplay_startup_errors = On // whether to display the error message of the startup process On the page, remember that several Core errors occurred during startup. this is to control whether these errors are displayed on the page. Log_errors = On // whether to record the error log log_errors_max_len = 1024 // The maximum length of the error log ignore_repeated_errors = Off // whether to ignore repeated error track_errors = Off // whether to use the global variable $ php_errormsg to record the last error xmlrpc_errors = 0 // whether to use the error message format of the XML-RPC to record the error xmlrpc_error_number = 0 // the value used as the XML-RPC faultCode element. Html_errors = On // whether to change the output function and other information to the HTML link docref_root = http: // manual/en // If html_errors is enabled, what is the root path of this link fastcgi. logging = 0 // whether to throw a php error to fastcgi

We are often asked what is the difference between error_reporting and display_errors? These two functions are completely different.

By default, PHP logs and standard output (if the fpm mode is used, standard output is the page)

The error_reporting parameter is of the error level. Indicates what level should an error be triggered. If we tell PHP that errors do not need to be triggered at all error levels, no logs or pages will display this error, which is equivalent to nothing happening.

Display_errors controls whether to display error messages in standard output

Log_errors controls whether to record error messages in logs.

Error_log is the location where error logs are displayed. This log is often overwritten in php-fpm, so it is often found that the error logs of cli and fpm are not in the same file.

The ignore_repeated_errors flag controls only one record if there are duplicate logs, such as the following program:

error_reporting(E_ALL);ini_set('ignore_repeated_errors', 1);ini_set('ignore_repeated_source', 1);$a = $c; $a = $c; //E_NOTICE//Notice: Undefined variable: c in /tmp/php/index.php on line 20 

There will be two NOTICE events, but now, there will be only one...

When track_errors is enabled, the last error message is stored in the variable. This may be useful for logging. But I think it's useless...

Html_errors and docref_root are both user-friendly configurations. after these two parameters are configured, if some information in the document is returned in the error message, it will become a link.

error_reporting(E_ALL);ini_set('html_errors', 1);ini_set('docref_root', "https://secure.php.net/manual/zh/");include("a2.php"); //E_WARNING 

Page display:

This allows you to quickly locate the cause of an error. Is it very human ~

Configuration in php-fpm

Error_log =/var/log/php-fpm/error. log // php-fpm's own log log_level = notice // php-fpm's own log record level php_flag [display_errors] = off // overwrite php. A configuration variable in ini that can be overwritten by ini_set in the program php_value [display_errors] = off // same as php_flagphp_admin_value [error_log] =/tmp/www-error.log // override php. A configuration variable in ini, cannot be overwritten by ini_set in the program php_admin_flag [log_errors] = on // same region = yes // whether to capture fpmworker output request_slowlog_timeout = 0 // slow log duration slowlog =/var/log /php-fpm/www-slow.log // slow log records

The php-fpm configuration also has an error_log configuration, which is often mixed with the error_log configuration in php. ini. But what they record is different,

The error_log of php-fpm only records the logs of php-fpm, such as when fpm is started and disabled.

The error_log in php. ini records the error log of the php program.

To overwrite the error_log configuration in php. ini in php-fpm, you need to use the following functions:

• Php_flag
• Php_value
• Php_admin_flag
• Php_admin_value

The two functions of the four functions admin indicate that after the variable is set, the variable cannot be re-assigned using ini_set in the code. Php_flag/value is still subject to ini_set in php code.

Slowlog is recorded by fpm. you can use request_slowlog_timeout to determine the duration of slow logs.

Summary

What we often confuse is the log problem, and why some levels of logs are not recorded in the log. The most important thing is to look at the three configurations error_log, display_errors, and log_errors, but when looking at the configuration, we should also pay attention to distinguish php. what is the configuration in ini, what is the configuration in the php-fpm.ini.

Well, I think there is basically no WTF problem that php logs cannot record when I understand these configurations.

Summary of PHP error mechanisms I will introduce you here, and hope to help you!

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.