Definition of debugging: Find and reduce the number of defects in the program through a certain method, so that it can work properly.
Here are some lessons about how to debug PHP programs.
One, PHP comes with the debugging function
1, self-brought error function
Two noun: the development environment is the developer in the development and debugging environment, the production environment is the end customer in the use of the online environment;
Development environment and production environment to set the error function separately.
(1) development environment
The development environment needs to open an error, the following is the configuration of php.ini and its description:
; This directive sets the error reporting level.
; Development Value:e_all | E_strict (Show all errors, warnings and notices including coding standards.)
error_reporting = E_all | E_strict
; This directive controls whether or not and where PHP would output errors,
; Notices and warnings too. Error output is very useful during development.
; Development Value:on
Display_errors = On
This way, you can find errors the first time in the development process.
Even a low-level error "notice:undefined variable:a in E:\phpspace\test.php on line 14", but the use of an undefined variable often hides the bug.
You would ask, what if I introduce an open-source class library and they throw a bunch of low-level bugs? General code quality of the class library, there is no "Notice" level of error. So it's also a way to identify the quality of a class library.
(2) production environment
The production environment cannot directly output the error, but is logged, the following is the configuration entry for php.ini and its description:
; It ' s recommended that errors is logged on production servers rather than
; have the errors sent to STDOUT.
Display_errors = Off
; Besides displaying errors, PHP can also log errors to locations such as a
; server-specific log, STDERR, or a location specified by the Error_log
; Directive found below. While errors should not being displayed on productions
; servers they should still being monitored and logging is a great the to does that.
; Production value:on
Log_errors = on
; Log errors to specified file.
Error_log =/path/to/php_error.log
Of course, the log written to the file is only a choice, there are other configurations can refer to the manual.
Production environment is to provide services to customers, you can not be on the above breakpoint, print output and other operations, so the log is a good choice.
2, some other language features, the use of functions
(1) less error control operator "@"
The function is to put "@" before a PHP expression, any error message that the expression may produce is ignored.
If a flaw occurs in this expression, no errors are seen from the output of PHP, which increases the difficulty of debugging. So you don't have to.
(2) Some functions are self-equipped with debug function
For example, this line of code:
$fp = Fsockopen ("www.example.com", $errno, $errstr, 30);
Developer debugging has been determined that the $fp is empty, the connection failed, there is a problem in this row, but why the connection failed?
The function is PHP's own and cannot be debugged in more depth. So the general function (mainly the network communication Class), will provide the debugging parameters themselves: $errno and $errstr. You can add a sentence:
if (! $fp) echo "$errstr ($errno) <br/>\n";
You can see why the connection failed.
These functions are: Fsockopen,pfsockopen,stream_socket_server,stream_socket_client and so on.
Some functions are used to debug a function, such as: Mysql_errno,socket_last_error,socket_strerror.
These only need to understand, encounter can think of using can.
Second, the introduction of debugging Tools
Debugging tools are available when you encounter complex problems. The more mature have xdebug, Zenddebugger.
Taking Xdebug as an example, it is able to control the style and array hierarchy of printouts, stack-up tracing errors, trace function calls, code execution overlay analysis, program profiling (Profiling), and remote debugging. See: http://xdebug.org/docs/.
Xdebug the first two functions of the original PHP debugging function has been improved, more convenient debugging.
Complex problems, debugging does not come out, may be the business of the problem, the following also said business logic debugging.
III. Debugging business logic errors
When the PHP script runs, there is no error, only that it has no grammatical errors, but does not indicate that it is not a business logic error.
Many of the business logic errors are not reflected in syntax errors, but the idea of debugging is similar to PHP's own debugging capabilities.
Here are some methods.
1, the most basic debugging method
First determine two things: The program expected results, the program does not meet the expected results;
Find code snippets related to the two results;
Read these snippets and try to find errors with the "naked eye";
Can not be found, you need to output some key variables, by checking whether their values are correct to determine where the error occurred;
Several attempts, and finally you can determine the point at which the error occurred.
You can also use tools such as xdebug to see changes in variable values, or to set breakpoints for debugging.
2. Record the running log
Some complex or special business, using the above method is inappropriate, such as: A can not be interrupted in the background run script. It is appropriate to record the running log in these cases.
Log points to choose, in addition to business more important points, usually error-prone places are: network connectivity and communication, system permissions issues.
3. Unit Test
Test the code in code, instead of throwing out the test code as if it were the 1th. To test-driven development.
This topic is relatively large, but it is suitable to put it here. Interested students can get to know.
Iv. Debugging non-functional errors
Non-functional errors, such as: Memory overflow causes the program to hang out, the efficiency of the problem caused the program is very slow, dead loop, and so on.
These problems, using the "naked eye" to check the code efficiency is too low.
So you can use the debugging tools to do the program profiling (Profiling), to check out the bottleneck of the program.
How to debug a PHP program