Introduction to various methods for debugging PHP programs, debugging php program _ PHP Tutorial

Source: Internet
Author: User
Tags coding standards perl interpreter sapi
Describes how to debug a PHP program. Introduction to various methods for debugging PHP programs, debugging definition of php program debugging: find and reduce the number of defects in the program through a certain method, so that it can work normally. Here we will introduce several methods to debug the PHP program.

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

I. debugging functions provided by PHP

1. built-in error reporting function

Two terms: development environment is the environment where developers are developing and debugging, and production environment is the online environment for end customers to use;
The error reporting function must be set separately in the development environment and production environment.

(1) Development Environment

An error must be reported when the development environment is enabled. the configuration items of php. ini and their descriptions are as follows:

The code is as follows:


; 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 immediately discover errors during the development process.

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

You will ask, what if I introduce open-source class libraries and they throw a bunch of low-level errors? Generally, class libraries with good code quality do not report "Notice" errors. This is also a method to identify the quality of a class library.

(2) production environment

In the production environment, errors cannot be output directly, but are logged. the configuration items of php. ini and their descriptions are as follows:

The code is as follows:


; It cocould be very dangerous in production environments.
; It's recommended that errors be logged on production servers rather
; Having the errors sent to STDOUT.
Display_errors = Off

; Besides displaying errors, PHP can also log errors to locations such as
; Server-specific log, STDERR, or a location specified by the error_log
; Directive found below. While errors shocould not be displayed on productions
; Servers they shoshould 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 one option. for other configurations, see the manual.

The production environment provides services to customers. you cannot perform breakpoint, print output, and other operations on it, so logs are a good choice.

2. use of other language features and functions

(1) less error control operator "@"

Its function is to place "@" before a PHP expression, and any error information that may be generated by this expression is ignored.

If a defect occurs in this expression and no errors are visible from the PHP output, this increases the difficulty of debugging. So you don't need it.

(2) some functions have the debug function.

For example, this line of code:

The code is as follows:


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

The developer confirmed that the connection failed because $ fp is null during debugging. why is the connection failed?

Functions are built in php and cannot be further debugged. Therefore, such a function (mainly for network communication) will provide its own debugging parameters: $ errno and $ errstr. You can add:

The code is as follows:


If (! $ Fp) echo "$ errstr ($ errno)
\ N ";

The reason for the connection failure is displayed.

These functions include fsockopen, pfsockopen, stream_socket_server, and stream_socket_client.

Some functions are used to debug a function, such as mysql_errno, socket_last_error, and socket_strerror.

All you need to know is how to use it.

II. introduce debugging tools

Debugging tools can be used to solve complex problems. The more mature ones are Xdebug and ZendDebugger.

Taking Xdebug as an example, it can control the print output style and array level, stack-based tracing errors, tracing function calls, code execution overwrite analysis, and program summary analysis (Profiling) remote debugging. For details, see http://xdebug.org/docs /.

The first two functions of Xdebug are improvements to the original debugging functions of PHP to facilitate debugging.

Complex problems, which cannot be debugged, may be business problems. The following describes the debugging of business logic.

3. debugging 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 indicate that it has no business logic errors.

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

The following are some methods.

1. Basic debugging methods

Determine the expected results of the program and the unexpected results of the program;
Search for code snippets related to the two results;
Read these code snippets and try to identify errors with the naked eye;
If no key variable is found, it is necessary to output some key variables and check whether their values are correct to determine where an error occurred;
After several attempts, you can determine at which point the error occurs.

You can also use Xdebug and other tools to view variable values or set breakpoints for debugging.

2. record running logs

Some complex or special services are not suitable for the above methods, such as running a script in the background that cannot be interrupted. It is appropriate to record running logs in these cases.

Log recording points must be selected. in addition to important business points, errors are usually caused by network connection and communication, and system permissions.

3. unit test

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

This topic is relatively large, but it is suitable to be mentioned here. If you are interested, you can learn about it.

IV. debugging non-functional errors

Non-functional errors, such as program crashes due to memory overflow, slow programs due to efficiency, and endless loops.

These problems are too inefficient to check the code with the naked eye.

Therefore, you can use the debugging tool to perform a program Profile Analysis (Profiling) to check the program bottleneck.


How to debug the php program

If there is no foundation, we do not recommend that you learn "database statement usage" from such mature programs. mature programs generally encapsulate similar functions into classes, the call contains a variety of usage methods and functions, which are generally difficult to understand.

Debugging for php programs

Objective: to configure the environment for running PHP (IIS + PHP + MYSQL + CF + Perl) and support php + cgi + asp + jsp
Operating system: windows2000 advance server (sp3) simplified Chinese version
Required software:
A: php-4.3.0-Win32.zip, download to the corresponding site, I upload files is not convenient, the following is the same.
2: ActivePerl-5.6.1.635-MSWin32-x86.msi, note, must download the installation file, that is to say. if you do not want to support cgi, you can skip the installation.
3: mysql-4.0.7-gamma-win.zip
4: MM ColdFusion MX (whether or not there is such a pull, I also pretend to play, mainly support PHP + MYSQL)

Everything is based on the sub-directory on my machine. assume that L: \ WEB is your site root directory on your machine.

Decompress php-4.3.0-win32.zip to the L: \ php directory.
Copy php4ts. dll in the php directory to the winnt \ system32 directory.
Copy php. ini. dist to the winnt directory and remove the extension. dist to get the php. ini file. Use Notepad to open and modify the two parameters:
1. search for extension_dir and point it to your PHP extensions directory. for example, if I am L: \ PHP \ extensions, modify it as follows:
Extension_dir = L: \ PHP \ extensions. you can modify extension when you want to support some extension libraries.
Save php. ini

Install mysql:
Open the mysql-4.0.7-gamma-win.zip package
Double-click setup.exe to run the program. during the installation process, pay attention to the mysql installation directory (of course, you can use the default directory). For example, I installed it in the L: \ mysql directory.
Run L: \ mysql \ bin \ mysqld-nt.exe, in fact, after the general installation is completed, the system automatically starts the service, if not assured to the "service" to see whether mysql started

I don't know whether to install the M $ Office to support asp. I have installed all the office after installing the system. it is estimated that asp is supported even if the system is not installed, (If you don't want it to support asp, you can ignore it)

Install the perl interpreter:
Run the ActivePerl-5.6.1.635-MSWin32-x86.msi, select the L: \ perl directory during installation, of course, you should also by default.

Install ColdFusion MX of MM. I installed it in the L: \ ColdFusionMX directory.

Configuration Server, Hey, I have to open photoshop to figure it here. that's hard to say.

Click Start> Program> Administrative Tools> Internet service manager.

Click here to view the large image

Click "default web site", right-click it, and select "properties" to open the dialog box.

Click here to view the large image

Click "main directory" and make the following settings: in "local path", select the folder on your machine as the root directory of the site. for example, if my folder is L: \ WEB, for example: click "configuration" in the lower right corner to bring up a new dialog box.

Select "add" in the new dialog box"

In the executable file, select L: \ php \ sapi \ php4isapi. dll. in the Browse dialog box, select *. dll. Otherwise, the directory under L: \ php \ sapi \ will not be visible. dll file. enter it in the extension after selecting the file. php, for example:

Click OK

In the above method, add L: \ perl... the remaining full text>

Definition of pipeline debugging: find and reduce the number of defects in the program through a certain method, so that it can work normally. Here is...

Related Article

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.