PHP Program Debugging method Summary _php Tutorial

Source: Internet
Author: User
I believe many friends have the experience of debugging programs, but many times debugging procedures is painful and lengthy process, it needs not only careful, but also need patience, avoid impatient air. But when it comes to finding out the problem and solving it smoothly, it gives people a thrill of joy. Here summarizes the author in the program debugging the use of principles, tools, and methods. It is necessary to note that some principled things, all languages are similar, but related to specific tools and some specific debugging methods, this is only related to web development content.

General principles:

1. Identify the cause of the problem:

The program needs to be debugged because of a problem with the program. The first goal of debugging is to find out why. Common debugging methods: When we face the entire complex system, we often do not know where the problem is, and this time can be excluded from the problem is not related to the exclusion of the area, gradually reduce the scope of debugging. Independent module method: Most of the program has a module structure, the suspect module is taken out separately, analog input corresponding operation data to see if there is a problem. Comparison method: If the program or system is based on a platform, you can first look at other platform-based programs or systems, to find out whether it is a platform problem or this program problem, many times this method is used to eliminate the hidden problems on the immature platform. Contrast method: For their own implementation of the program, may already have a corresponding open source module, you can find out the source of these programs, and their own procedures to compare, so not only save time, but also can draw on the good of others.

2. Problem locating:

The cause of the problem is so varied that you can only know what the problem is and what might be related to it at this time, and you need to find the code snippet that appears. In order to do this, the general development environment provides us with a number of practical tools that allow you to view the input and output of the program step-by-step, locating the problem according to the input and output of each step.

There are some very powerful people who can read the code and find out where the problem is. But many of the problems are due to the debugger's own negligence, and in such a way to find the problem, it is almost impossible, because it has formed a fixed mindset.

3. Problem solving

Finding the cause of the problem and the corresponding line of code, it is often a relatively simple matter to solve it, because these problems tend to be negligent or poorly considered. But sometimes this is not always the case, but because of the external environment, such as the increase in the number of visitors to your website, you did not consider the data pressure, but also did not consider the high degree of concurrency problems, this time the problem solved you feel more difficult. To solve such problems, not only to focus on the process, but also from the system architecture, to synthesize all aspects of knowledge, to carry out a comprehensive consideration.

Common Debugging Tools for Web development

As the saying goes: The work wants good its stone, its prerequisite. For program debugging, you must also use external tools. Here are a few of the tools that are commonly used in developing Web applications:

Xdebug, xhprof:php Performance Debugging tool. They are all an extension of PHP and can be easily installed and configured. These two tools are mainly the internal health of your program, call the function in a concise way to tell you. Allows you to place too much resources on where the program is, and where it needs to be optimized at a glance. By the way, Xdebug generated profile files are generally viewed with wincachegrid.

firephp, Firbug: The former is mainly PHP debugging tools, and the latter is an essential tool for web development, it can not only view the request, but also debug js,css.

You can use HttpWatch under IE. If you need to do deep analysis of your data, you can use the Grab kit tool.

Commonly used debugging functions in PHP: Error_report, Var_dump, Print_r, Var_export

Common debugging Techniques for Mysql,apache/nginx: Analyzing their log files is also very important. Common commands for viewing log files under Linux: Cat, More, less, grep, awk, sed

Common debugging techniques and methods

Below, I debug the program to introduce the specific debugging methods and techniques.

Example 1: This is a PHP developed game engine, the main feature provides dual-player mobile position, status, passing, skills and other functions to control the entire program through the XML file to save results. Suddenly one day, there are two users playing, the returned XML does not conform to the normal structure. From the results of the return, the player's moving position is somewhat abnormal, some of the steps are missing, and decided to look for the game AI, but there is nothing unusual here. Finally, the input player data is printed out, and then the data produced in the middle of the printing, combined with the process logic of the program, found that the two users have the same player, so that in the process of processing, two people players appear chaotic. Once you have found the cause, change the player's logo to the team and the player as a joint decision.

Example 2: As soon as the program is online, some enthusiastic friends like to do some stress tests on the program. A sudden 20w request is a large static resource that directly causes the hard drive to crash. In this case, you must review the access log before you know what happened. Otherwise, you can't find the reason why you're being attacked by someone else. Of course, for such attacks, the static resources are directly placed on the CDN. In addition, as the number of access users increased, the system pressure increased, the reaction gradually slowed down, we have to consider: previously only 0.000 milliseconds to process the program, now requires 0. A few seconds. The concurrency design of the program is very important at this point. Failure to do so will result in abnormal data conditions.

Example 3: Using an external software exception: for example, when using memcached, if a station is not started or is not available. This time nginx will generally be used for some memcache request return 502, is not feeling a bit unreasonable, you can consider rewriting the memcahce PHP extension

http://www.bkjia.com/PHPjc/371989.html www.bkjia.com true http://www.bkjia.com/PHPjc/371989.html techarticle I believe many friends have the experience of debugging programs, but many times debugging procedures is painful and lengthy process, it needs not only careful, but also need patience, avoid impatient air. But when ...

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