A variety of ways to debug PHP programs introduce _php techniques

Source: Internet
Author: User
Tags php script

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

The debugging function of PHP with self

1, with the error function

Two nouns: development environment is the development and debugging of developers in the environment, production environment is the ultimate customer in the online environment;
The development environment and production environment to set the error function separately.

(1) Development environment

The development environment needs to turn on the error, the following is the PHP.ini configuration item and its description:

Copy Code code as follows:

; This is directive sets the error reporting level.
; Development Value:e_all | E_strict (Show all errors, warnings and notices including coding.)
error_reporting = E_all | E_strict

; This is 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

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

Even if it 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 a bug.

You ask, what if I introduce an Open-source class library and they throw a bunch of low-level bugs? General code good quality class library, there is no "Notice" level of the 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 rather log it, the following is the PHP.ini configuration item and its description:

Copy Code code as follows:

; It could be very the dangerous in production environments.
; It ' s recommended that errors is logged on production servers rather
; 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 is displayed on productions
; Servers they should still be monitored and logging are a great way to do.
; Production Value:on
Log_errors = On

; Log errors to specified file.
Error_log =/path/to/php_error.log

Of course the log is written to a file only as a choice, and there are other configurations to refer to the manual.

Production environment is to provide services to customers, you can not be on the above breakpoints, printouts and other operations, so the log is a good choice.

2, the use of some other language features, functions

(1) Less error control operator "@"

The effect is to place "@" before a PHP expression, and any error messages that the expression may produce are ignored.

If a flaw occurs in this expression, there is no error from the output of PHP, which increases the difficulty of debugging. So you can use it without it.

(2) Some functions with debug function

Like this line of code:

Copy Code code as follows:

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

Developer debugging has determined that the $fp is empty, the connection failed, is a problem in this line, but why the connection failed?

The function is PHP-led, and cannot be further debugged. So generally such functions (mainly network communication Class), will provide debugging parameters themselves: $errno and $errstr. You can add one sentence:

Copy Code code as follows:

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.

There are also some functions to debug a function, such as: Mysql_errno,socket_last_error,socket_strerror and so on.

These only need to understand, encounter can think of use.

Second, the introduction of debugging Tools

When you encounter complex problems, you can use the debugging tools. The more mature have xdebug, Zenddebugger.

Take Xdebug as an example, it can: control the style of printout and array hierarchy, stack-type tracking error, tracking function call, code execution coverage Analysis, program summary analysis (Profiling), 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.

Third, debugging business logic errors

When the PHP script runs, there is no error, it can only be said that there is no grammatical errors, but it does not mean that the business logic is not wrong.

Many business logic errors are not reflected in syntax errors, but debugging ideas and PHP with debugging functions almost.

Here are some methods.

1, the most basic debugging methods

Determine two things first: the expected results of the program, the procedure is not in line with the expected results;
Look for code snippets related to two results;
Read the code snippet and try to find the error with the naked eye.
Can not find out, you need to output some key variables, by checking their value is correct to determine where the error occurred;
Several attempts, and eventually you can determine which point 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, with the above method is not appropriate, such as: A Can not be interrupted by the background run script. It is appropriate to log running logs in these situations.

Log points to have a choice, in addition to the business more important points, usually error-prone places are: network connectivity and communication, system permissions issues.

3. Unit Test

Testing your code in code, rather than debugging it as a 1th, will throw out the test code. To test-driven development.

This topic is quite large, but it is suitable for mention here. Interested students can go to understand.

Iv. Debugging Non-functional Errors

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

These problems, using the "naked eye" to check the code is too inefficient.

So you can use the debugging tools to do the program's Profile analysis (Profiling), from which the bottleneck of the program is checked out.

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.