Introduction of various methods for debugging PHP program

  • 2021-07-24 10:35:04
  • OfStack

Definition of debugging: Find and reduce the number of defects in the program by a certain method, so that it can work normally.
Here are some experiences on how to debug PHP programs.

1. PHP debugging features

1. Its own error reporting function

Two nouns: the development environment is the environment where developers are developing and debugging, and the production environment is the online environment used by end customers;
The error reporting function should be set separately in the development environment and the production environment.

(1) Development environment

The development environment needs to turn on error reporting. Here are the configuration items for php. ini and their descriptions:


; 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 will output errors,
; notices and warnings too. Error output is very useful during development.
; Development Value: On
display_errors = On

In this way, you can find errors in the first time during the development process.

Even if there is a low-level error "Notice: Undefined variable: a in E:\ phpspace\ test. php on line 14", the use of an undefined variable often hides bug.

You may ask, what if I introduce an open source class library and they throw a heap of low-level errors? 1 class library with good code quality, and there is no "Notice" level error reporting. So this is also a way to identify the quality of a class library.

(2) Production environment

The production environment cannot output errors directly, but instead logs them. Here are the configuration items for php. ini and their descriptions:


; It could be very dangerous in production environments.
; It's recommended that errors be logged on production servers rather than
; having 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 be displayed on productions
; servers they should still be monitored and logging is a great way to do that.
; Production Value: On
log_errors = On ; Log errors to specified file.
error_log = /path/to/php_error.log

Of course, writing logs to files is only an option, and there are other configurations to refer to the manual.

The production environment is to provide services to customers, you can't do breakpoints, printouts and other operations on it, so logs are a good choice.

2. Use of other language features and functions

(1) Use less error control operator "@"

Its effect is to place "@" before an PHP expression, and any error messages that may be generated by the expression are ignored.

If a defect occurs in this expression, no error can be seen in the output of PHP, which makes debugging more difficult. So don't use it if you can.

(2) Some functions come with debug functionality

For example, this line of code:


$fp = fsockopen( " www.example.com " , 80, $errno, $errstr, 30);

When the developer debugged, it was determined that $fp was empty and the connection failed. There was something wrong with this line, but why did the connection fail?

The function comes with php, so it cannot be debugged in depth. So one such function (mainly network communication class) will provide its own debugging parameters: $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, etc.

There are also some functions for debugging a function, such as mysql_errno, socket_last_error, socket_strerror and so on.

These just need to know, and when you come across them, you can think of using them.

2. Introduce debugging tools

When encountering complex problems, you can use debugging tools. The mature ones are Xdebug and ZendDebugger.

Taking Xdebug as an example, it can: control the style and array level of printout, stack trace error, trace function call, code execution coverage analysis, program profiling (Profiling), and remote debugging. See: http://xdebug.org/docs/ for details.

The first two functions of Xdebug are improved on the original debugging function of PHP, which is more convenient for debugging.

Complex problems, debugging can not come out, may be business problems, the following also said business logic debugging.

3. Debug business logic errors

When the PHP script runs, there are no errors. It can only be said that it has no syntax errors, but it cannot be said that it has no business logic errors.

Many business logic errors are not reflected in syntax errors, but the debugging idea is similar to that of PHP.

Here are some methods.

1. The most basic debugging method

First, determine two things: the expected result of the program, and the current result of the program does not meet the expected result;
Look for code snippets related to both results;
Read these code snippets and try to find errors with the naked eye;
If you can't find it, you need to output 1 key variables, and judge where the error occurred by checking whether their values are correct;
After several attempts, you can finally determine where the error occurred.

You can also use tools such as Xdebug to view changes in variable values or set breakpoints for debugging.

2. Record the running log

Some complex or special business, using the above method is not appropriate, such as: 1 can not be interrupted background running script. In these cases, it is appropriate to record the operation log.

Log points to have a choice, in addition to business more important points, usually prone to errors are: network connection and communication, system authority issues, etc.

3. Unit testing

Test the code with code, instead of throwing away the test code after debugging as in point 1. 1. Test-driven development.

This topic is relatively big, but it is suitable to mention it here. Interested students can learn about it.

4. Debug non-functional errors

Non-functional errors, such as: memory overflow causes the program to hang up, efficiency problems cause the program to be very slow, dead loop and so on.

For these problems, it is too inefficient to check the code with naked eyes.

So you can use debugging tools to do program summary analysis (Profiling), from which to check out the bottleneck of the program.


Related articles: