PHP Error mechanism knowledge Rollup _php instance

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

PHP's error mechanism is also very complex, did a few years of PHP, also did not carefully summed up, now to fill in this lesson.

Special Note: The PHP version of the article uses 5.5.32

Error level of PHP

First you need to know what bugs are in PHP. Up to php5.5, a total of 16 error levels

Note: When you try the following code, be sure to open the Error_log:

Error_reporting (E_all);

E_error

This error is fatal error, will display fatal error in the page, when this error occurs, the program can not continue to carry out the

Error Example:

Fatal error:call to undefined function hpinfo () in/tmp/php/index.php in line 5

Note that this level is also triggered if there are exceptions that are not caught.

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

E_warning

This error is only a warning, does not terminate the script, the program will continue, 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

E_notice

This kind of error 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 wrong code may not be a problem anywhere else, except in the current context.

For example, the $b variable does not exist, and we assign it to another variable

notice:undefined variable:b in/tmp/php/index.php on line 9

E_parse

This error occurs at compile time, and syntax errors are found during compile time and cannot be parsed.

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

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

E_strict

This error was introduced after PHP5, and your code can be run, but not the PHP proposal.

Like passing the + + symbol in function parameters

Strict standards:only variables should is passed by reference in line
/function Change (in/tmp/php/index.php MP $var) {
$var + +
}
$var = 1;
Change (+ + $var);

E_recoverable_error

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

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

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, defined in/tmp/php/index.php on line,
class A {
}
class B {
}
function te Stcall (A $a) {
}
$b = new B ();

e_deprecated

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

For example, Curl's curlopt_postfields use \ @FILENAME to upload files

Deprecated:curl_setopt (): The usage of the @filename API for file uploading is deprecated. The Curlfile class instead in/tmp/php/index.php on line
$ch = Curl_init ("HTTP://WWW.REMOTESITE.COM/UPL Oad.php ");

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, using Trigger_error, and here is the equivalent of a slit to the user to touch out various types of errors. This is a good way to escape from a try catch exception.

Trigger_error ("Cannot divide by zero", e_user_error);
E_user_error
//e_user_waring
//E_user_notice

E_all

E_strict all errors and warnings out of the information.

Error control

There are many configurations and parameters in PHP that can control errors, as well as error log display. The first step is what we need to know about the wrong configuration in PHP.

According to PHP+PHP-FPM's model, what will affect the PHP error display is actually there are two profiles, 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//Reporting error level, what level of
Error_log =/tmp/php_errors.log//php error displayed in log position
display_errors = ON//Yes Whether the error is displayed on the output, this output may be a page, or it may be stdout
display_startup_errors = ON/If the error message for the startup process is displayed on the page, remember that there are several core types of errors that occur when the startup occurs, This is to control whether these errors display the page.
log_errors = ON//whether to log the error log
Log_errors_max_len = 1024//error log maximum length ignore_repeated_errors = off//
whether duplicate errors are ignored
track_errors = off//whether to use global variable $php_errormsg to record the last error
xmlrpc_errors = 0//Whether to log errors using XML-RPC error message format
XMLRPC_ Error_number = 0//is used as the value of the XML-RPC faultcode element.
html_errors = ON//whether to turn information such as functions in the output into HTML links
docref_root = http://manual/en///If Html_errors is turned on, what is the root path of this link

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. What level of representation should trigger the error. If we tell PHP that all error levels do not need to trigger the error, then neither the log nor the page will show the error as if nothing had happened.

Display_errors is to control whether or not to display error messages in standard output

Log_errors controls whether you want to log error messages in the logs.

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 control is that if there is a duplicate log, then only one record, 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

There would have been two times notice, but now, only once ...

Track_errors opening will store the last error message in the variable, which may be useful when it comes to journaling. But I think it's really no use ...

Html_errors and Docref_root Two is a very humane configuration, 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/");

Page display:

Allows you to quickly locate where we are wrong. is not very human nature ~

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//overwrite a configuration variable in php.ini, which can be overridden by Ini_set in the program
php_value[display_errors] = off//same php_flag
Php_admin_ Value[error_log] =/tmp/www-error.log//Overwrite a configuration variable in php.ini and cannot be overwritten by Ini_set in the program
php_admin_flag[log_errors] = ON// Same Php_admin_value
catch_workers_output = yes//whether to crawl Fpmworker output
request_slowlog_timeout = 0//Slow log length

There is also a error_log configuration in the PHP-FPM configuration, which is often confused with error_log configuration in php.ini. But the things they record are not the same,

PHP-FPM's error_log only logs php-fpm itself, such as FPM startup, shutdown.

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

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 show that after this variable is set, you cannot use Ini_set to reassign the variable in code. And Php_flag/value is still in the PHP code ini_set.

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

Summarize

What we often confuse is the log problem and why some levels of logging are not recorded in the log. The most important thing is to see Error_log,display_errors, log_errors These three configurations, just look at the configuration, we also need to pay attention to distinguish php.ini inside the configuration is what, Php-fpm.ini inside the configuration is what.

Well, I think understanding these configurations, basically there is no PHP log can not record the problem of WTF.

About the error mechanism of PHP knowledge of the small series on the introduction to everyone here, 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.