Home  >  Article  >  Backend Development  >  Introduction to various methods of debugging PHP programs, debugging PHP programs_PHP tutorial

Introduction to various methods of debugging PHP programs, debugging PHP programs_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:55962browse

Introduction to various methods of debugging PHP programs, debugging PHP programs

The definition of debugging: using certain methods to find and reduce the number of defects in the program so that it can work properly.
Here are some experiences on how to debug PHP programs.

1. PHP’s own debugging function

1. Built-in error reporting function

Two terms: the development environment is the environment where developers develop and debug, and the production environment is the online environment used by end customers;
The development environment and production environment must set up error reporting functions separately.

(1) Development environment

The development environment needs to enable error reporting. The following are the configuration items and descriptions of php.ini:

Copy code 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

This way you can find errors as soon as possible during the development process.

Even if it is a low-level error "Notice: Undefined variable: a in E:phpspacetest.php on line 14", the use of an undefined variable often hides bugs.

You may ask, what if I introduce an open source class library and they throw a bunch of low-level errors? Generally, class libraries with good code quality do not report "Notice" level errors. 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 records them in the log. The following are the configuration items and descriptions of php.ini:

Copy code The code is as follows:

; 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 a file is only an option. There are other configurations, please refer to the manual.

The production environment provides services to customers. You cannot perform breakpoints, printout, etc. on it, so logs are a good choice.

2. Use of other language features and functions

(1) Use the error control operator "@" less often

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

If a flaw occurs in this expression, no error will be visible in the PHP output, which makes debugging more difficult. So don’t use it if you can.

(2) Some functions have their own debug function

For example, this line of code:

Copy code The code is as follows:

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

The developer has determined during debugging that $fp is empty and the connection fails. There is a problem with this line, but why does the connection fail?

The function comes with PHP and cannot be debugged in depth. Therefore, generally such functions (mainly network communication types) will provide their own debugging parameters: $errno and $errstr. You can add:

Copy code The code is as follows:

if (!$fp) echo “$errstr ($errno)
n”;

You can see the reason why the connection failed.

These functions are: fsockopen, pfsockopen, stream_socket_server, stream_socket_client, etc.

There are also some functions used for debugging a function, such as: mysql_errno, socket_last_error, socket_strerror, etc.

You just need to understand these and use them whenever you encounter them.

2. Introduction of debugging tools

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

Taking Xdebug as an example, it can: control the print output style and array level, stack error tracking, function call tracking, code execution coverage analysis, program profiling, and remote debugging. For details, see: http://xdebug.org/docs/.

The first two functions of Xdebug are improvements to PHP’s original debugging functions to make debugging more convenient.

Complex problems that cannot be debugged may be business problems. The debugging of business logic is also discussed below.

3. Debugging business logic errors

When the PHP script runs without any errors, it can only be said that it has no grammatical errors, but it does not mean that there are no errors in its business logic.

Many business logic errors will not be reflected in syntax errors, but the debugging idea is similar to the debugging function that comes with PHP.

Here are some methods.

1. The most basic debugging method

First determine two things: the expected results of the program, and the current results of the program that do not meet the expectations;
Look for code snippets related to both outcomes;
Read these code snippets and try to spot the errors "with the naked eye";
If you cannot find it, you need to output some key variables and check whether their values ​​are correct to determine where the error occurred;
After several attempts, you can eventually determine the point at which the error occurred.

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

2. Record operation log

For some complex or special businesses, the above method is not suitable, for example: a background running script that cannot be interrupted. In these cases, it is more appropriate to record running logs.

Be selective about the points you keep in logs. In addition to the more important points in business, there are usually areas where errors are likely to occur: network connection and communication, system permission issues, etc.

3. Unit test

Test the code with code instead of throwing away the test code after debugging like the first point. Test-driven development.

This topic is relatively big, but it’s appropriate to mention it here. Interested students can learn more.

4. Debugging non-functional errors

Non-functional errors, such as: memory overflow causing the program to hang, efficiency issues causing the program to be very slow, endless loops, etc.

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

So you can use debugging tools to do profiling of the program and check out the bottlenecks of the program.

How to debug php program

If you don’t have a certain foundation, it is not recommended that you learn "the use of database statements" from such mature programs. Mature programs usually encapsulate the usage and functions with similar functions into classes, and call them together. There are various usage methods and functions. Generally, it will be difficult for you to understand them without a certain foundation

What to use to debug php program

Purpose: Configure the environment for 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:
1: php-4.3.0-Win32.zip, download from the corresponding site. It is inconvenient for me to upload files, the same below.
Two: ActivePerl-5.6.1.635-MSWin32-x86.msi. Note that you must download the installation file, that is, .msi. Do not download the original code. I will not make the original code. Install this to support cgi. If you don’t want to support cgi, you don’t need to download this installation
Three: mysql-4.0.7-gamma-win.zip
Four: MM ColdFusion MX (it doesn’t matter whether you have this or not, I’m just pretending to do it, mainly Support PHP+MYSQL)

Everything is based on the directory on my machine. Assume that L:\WEB on your machine is the root directory of your site

Place php-4.3.0-Win32. Unzip the 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 php .ini file, open it with Notepad and modify the two parameters:
1. Search extension_dir and point it to your PHP extensions directory. For example, mine is L:\PHP\extensions. I modify it like this:
extension_dir = L:\PHP\extensions You can modify the extension when you want to support some extension libraries
Save php.ini

Install mysql:
Open mysql-4.0.7-gamma-win.zip Compressed package
Double-click setup.exe to run the program. During the installation process, pay attention to selecting the installation directory of mysql (of course, you can also choose the default directory). For example, I installed it in the L:\mysql directory
Run L:\ mysql\bin\mysqld-nt.exe. In fact, the system usually starts the service automatically after the installation is completed. If you are not sure, go to "Services" to see if mysql is started.

Install M$ Office. I don't know whether to install it or not. I don’t know if this thing only supports ASP. Anyway, after I installed the system, I installed all the offices. Haha, I guess it supports ASP even without the installation. (If you don’t want it to support ASP, you can leave it alone.)

Install the perl interpreter:
Run ActivePerl-5.6.1.635-MSWin32-x86.msi. Select the L:\perl directory during the installation process. Of course, you can choose the default.

To install MM's ColdFusion MX, I installed it in the L:\ColdFusionMX directory

Configuring the server, hehe, I had to open photoshop to draw the picture after writing this, it's hard to say

Click Start->Programs->Administrative Tools->Internet Service Manager

Click this link to view the larger image

Click "Default web site" and then right-click It, select "Properties", open the dialog box

Click this link to view the larger image

Click "Home Directory" and make the following settings: Select your machine in "Local Path" The folder to be used as the root directory of the site, for example, mine is L:\WEB, as shown below: Then click "Configure" on the lower right, a new dialog box will pop up

In the new dialog box, select "Add" ”

Browse and select L:\php\sapi\php4isapi.dll in the executable file. Note that you must select *.dll as the file type in the browse dialog box, otherwise you will not see L:\php\sapi\ .dll file in the directory. After selecting, enter .php in the extension, as shown below:

Click OK

Add L:\perl as above..... .The rest of the full text>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/907282.htmlTechArticleIntroduction to various methods of debugging PHP programs, debugging the definition of debugging PHP programs: through certain methods, find in the program And reduce the number of defects so that it can work properly. Let’s talk about it here...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn