Summary of PHP error mechanisms

Source: Internet
Author: User
Tags deprecated fpm parse error php error stack trace try catch what php

Php's error mechanism is also very complex, did a few years of php, also did not carefully summed up, now fill this Lesson.

Special Note: The PHP version of the article uses 5.5.32

Error level for PHP

First you need to know what PHP errors Are. Up to php5.5, a total of 16 error levels

Note: when trying the following code, make sure to open error_log:

Error_reporting (e_all);
Ini_set (' display_errors ', ' on ');
E_error

This error is fatal and will show fatal error on the page, and when this error occurs, the program will not be able to carry On.

Error example:

Fatal error:call to undefined function hpinfo () in/tmp/php/index.php on line 5
Hpinfo (); E_error
Note that this level is also triggered if there are uncaught exceptions.

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 5
throw new \exception ("test Exception");
E_warning

This error is just a warning, does not terminate the script, the program will continue, and the error message displayed is Warning. For example, include a file that does not Exist.

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 7
Include ("a.php"); E_warning
E_notice

This is a little bit more subtle, suggesting that this place should not be written in this way. This is also a run-time error, and the code for this error may not be a problem elsewhere, just a problem in the current Context.

For example, 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 at compile time, syntax errors are found during compilation, and parsing is not possible.

For example, The following z is not set to a variable.

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

This error was introduced after PHP5, your code can be run, but not Php's suggested Notation.

For example, the function parameter pass + + symbol

Strict standards:only variables should be passed by reference in/tmp/php/index.php on line 17
function Change (& $var) {
$var + = 10;
}

$var = 1;
Change (+ + $var);
E_strict
E_recoverable_error

This level is actually an error level, but it is expected to be captured, and if not caught by error handling, the performance is the same as E_error.

Often occurs when a parameter defines a type, but when called, the error type is passed IN. Its error alert is also more than E_error fatal error in front of a catachable Word.

Catchable fatal Error:argument 1 passed to Testcall () must is an instance of A, instance of B given, called in/tmp/php /index.php on line PNS and defined in/tmp/php/index.php on line 33
Class A {
}

Class B {
}

function Testcall (A $a) {
}

$b = new B ();
Testcall ($b);
e_deprecated

This error indicates that you have used an older version of the function, and the later version of the function may be disabled or not maintained.

such as Curl's curlopt_postfields use \ @FILENAME to upload the file method

Deprecated:curl_setopt (): The usage of the @filename API for file uploading is Deprecated. 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, which occurs during the initialization of PHP.

e_compile_error, e_compile_warning

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

e_user_error, e_user_warning, e_user_notice, e_user_deprecated,

These errors are made by the user, and using trigger_error, This is the equivalent of a hole that gives the user a variety of error types. This is a good way to evade the try Catch Exception.

Trigger_error ("cannot divide by zero", e_user_error);
E_user_error
E_user_waring
E_user_notice
e_user_deprecated
E_all

E_strict all error and warning messages that go Out.

Error control

There are many configurations and parameters in PHP that can control errors, as well as errors in the log display. first, What do we need to know about the configuration of the error in php?

We follow the PHP+PHP-FPM model, will affect the PHP error display is actually two configuration files, one is the PHP itself configuration file php.ini, The other is PHP-FPM configuration file, php-fpm.conf.

Configuration in the php.ini

error_reporting = E_all//report Error level, what level
Error_log =/tmp/php_errors.log//error in PHP displays the log location
Display_errors = On//whether The error is displayed on the output, this output may be a page, or it may be stdout
Display_startup_errors = On//whether The error message of the startup process is displayed on the page, remember that there are several core types of errors that occur at the start time, this is to control whether these errors are displayed on the Page.
log_errors = on//whether to log error logs
Log_errors_max_len = 1024//maximum length of error log
Ignore_repeated_errors = Off/ignore Duplicate errors
Track_errors = Off//whether to use global variable $php_errormsg to record the last error
Xmlrpc_errors = 0//whether errors are logged using the XML-RPC error message format
Xmlrpc_error_number = 0//used As the value of the Xml-rpc faultcode Element.
Html_errors = On//whether The functions in the output and other information become HTML links
Docref_root = http://manual/en///if Html_errors is turned on, what is the root path of this link?
Fastcgi.logging = 0//whether to throw PHP errors into fastcgi
We are often asked, what is the difference between error_reporting and display_errors? The two functions are completely different.

PHP defaults to the log and standard output (if the FPM mode standard output is the Page)

The error_reporting parameter is the error level. The level that represents what you should trigger the Error. If we tell PHP that all error levels do not need to trigger an error, then neither the log nor the page will show this error, which would be equivalent to nothing happening.

Display_errors is to control whether to display error messages in standard output
Log_errors is the control of whether to log error messages.

Error_log is the location where the error log is displayed, which is often rewritten in php-fpm, so it is often found that the CLI and FPM error logs are not in the same file.

Ignore_repeated_errors This tag controls if there is a duplicate log, then only one is logged, 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
Would have appeared two times notice, but now, will only appear once ...

Track_errors Open will store the last error message in a variable, which may be useful for logging. But I think it's really useless.

Html_errors and Docref_root Two is a very humane configuration, Urumqi Lou Feng After configuring these two parameters, we return the error message if there is some information in the document, it will become a link form.

Error_reporting (e_all);
Ini_set (' html_errors ', 1);
Ini_set (' docref_root ', "https://secure.php.net/manual/zh/");

Include ("a2.php"); E_warning
Allows you to quickly locate where we are wrong. is not very human ~

Configuration in the PHP-FPM

Error_log =/var/log/php-fpm/error.log//php-fpm own log
Log_level = notice//php-fpm own logging level
php_flag[display_errors] = off//overrides A configuration variable in php.ini, which can be overridden by Ini_set in the program
php_value[display_errors] = Off//with Php_flag
php_admin_value[error_log] =/tmp/www-error.log//overwrite A configuration variable in the php.ini and not be overwritten by Ini_set in the program
php_admin_flag[log_errors] = On//with Php_admin_value
Catch_workers_output = Yes//whether to grab fpmworker output
Request_slowlog_timeout = 0//slow Log Duration
Slowlog =/var/log/php-fpm/www-slow.log//slow Logging
There is also a error_log configuration in the php-fpm configuration, which is often confused with the error_log configuration in Php.ini. But what they record is not the same, Php-fpm's error_log only logs php-fpm itself, such as FPM startup, off.

The Error_log in php.ini is the error log that records the PHP program itself.

Then in PHP-FPM to overwrite the error_log configuration in the php.ini, 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 show that after this variable is set, Urumqi Lou Feng www.jsgren.com/forum-88-1.html cannot use Ini_set in the code to reassign the Variable. Php_flag/value is still based on the ini_set in the PHP code.

Slowlog is the FPM record, you can use the Request_slowlog_timeout setting to determine the length of the slow log.

Summarize

What we often confuse is log issues, and why some levels of logging are not recorded in the log. The main thing is to see error_log,display_errors, log_errors these three configurations, just look at the configuration, we also have to pay attention to distinguish php.ini inside the configuration is what, Php-fpm.ini inside the configuration is What.

well, I think to understand these configurations, basically there is no PHP log can not record the WTF Problem.

Summary of PHP error mechanisms

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.