debugging PHP Program A variety of methods introduced, debugging PHP program _php Tutorial

Source: Internet
Author: User
Tags coding standards perl interpreter sapi

debugging PHP programs to introduce a variety of methods, debugging PHP program


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:
Copy the Code code 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 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, and the following is the configuration entry for php.ini and its description:
Copy the Code code as follows:
; It could is very dangerous in production environments.
; 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 be monitored and logging are a great a-to-do.
; 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:
Copy the Code code as follows:
$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:
Copy the Code code as follows:
if (! $fp) echo "$errstr ($errno)
\ 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

If there is no certain basis, do not recommend that you learn from this kind of mature program "database statement use", mature programs will often use similar functions, functions encapsulated into classes, calls, the collection of a variety of use methods and functions, generally do not have a certain basis you understand will be some difficulties

PHP program with what to debug

Purpose: Configure the environment running PHP (IIS+PHP+MYSQL+CF+PERL) to support php+cgi+asp+jsp, etc.
Operating system: Windows2000 Advance Server (SP3) Simplified Chinese version
Need related software:
A: Php-4.3.0-win32.zip, to the corresponding site download, I upload files inconvenient to the same.
II: ACTIVEPERL-5.6.1.635-MSWIN32-X86.MSI, note that the installation file must be downloaded, that is, the. msi, do not under the original code, I will not get the original code, add this support CGI, if you do not want to support CGI can also not download this installation
Three: Mysql-4.0.7-gamma-win.zip
Four: MM ColdFusion MX (There is no this doesn't matter pull, I also pretend to play, mainly support Php+mysql)

Everything is on my machine catalog as standard, assuming you on the machine L:\WEB for your site root directory

Extract the Php-4.3.0-win32.zip into the L:\php directory
Copy php directory under Php4ts.dll to Winnt\System32 directory
Copy the php.ini.dist to the Winnt directory, and remove the extension. Dist, get the php.ini file, open with Notepad to modify the two parameters:
1. Search Extension_dir, point it to your PHP extensions directory, such as I am L:\PHP\extensions, I modified:
Extension_dir = L:\PHP\extensions These can be modified when you want to support some extension libraries extension
Save PHP.ini

Install MySQL:
Open Mysql-4.0.7-gamma-win.zip Compressed Package
Double-click the setup.exe to run the program, during the installation process attention to select the MySQL installation directory (of course, you want to default directory also can), such as I installed in the L:\mysql directory
Run L:\mysql\bin\mysqld-nt.exe, in fact, the general installation is completed after the system automatically start service, if not rest assured to "service" to see if MySQL started

Install m$ Office, do not know whether to install this thing to support ASP I do not know, anyway, I installed the system after the installation of Office, hehe, estimated not installed also support ASP, (if you do not want it to support ASP can not go to the tube it)

To install the Perl interpreter:
Run Activeperl-5.6.1.635-mswin32-x86.msi, select the L:\perl directory during installation, and of course you'll have to do it by default.

MM ColdFusion MX, I put it in the L:\ColdFusionMX directory.

Configure the server, hey, write to this side have to open Photoshop figure, said not to say

Click Start-Programs-management tools-Internet Services Manager

Click this link to view a larger image

Click Default Web site, then right-click it, select Properties, open the dialog box

Click this link to view a larger image

Click "Home Directory" to do the following: In the "Local path" Select the folder on your machine to be the site root directory, such as my is L:\WEB, such as: And then click on the bottom right of the "configuration", pop Up a new dialog box

In the New dialog box, select Add

Browse to select L:\php\sapi\php4isapi.dll in the executable, note the file type in the Browse dialog box to select *.dll, otherwise you will not see the. dll file in the L:\php\sapi\ directory Oh, when selected, enter in the extension. PHP, such as:

Click OK

As the above method in adding L:\perl ... Remaining full text >>

http://www.bkjia.com/PHPjc/907282.html www.bkjia.com true http://www.bkjia.com/PHPjc/907282.html techarticle Debugging PHP program to introduce a variety of methods, debugging the definition of PHP program debugging: Through a certain way, in the program to find and reduce the number of defects, so that it can work properly. Here's to say a ...

  • 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.