Describes how to debug a 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe Code is as follows:
If (! $ Fp) echo "$ errstr ($ errno) <br/> \ 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>