Home > Article > Backend Development > Summary of PHP program debugging methods_PHP tutorial
I believe that many friends have the experience of debugging programs. However, debugging programs is often a painful and long process; it requires not only care, but also patience, and avoid being impatient. But when the problem is found and solved successfully, it will give people extremely exciting joy. Here is a summary of the principles, tools, and methods that the author uses in program debugging. What needs to be explained here is that some principle things are similar in various languages, but when it comes to specific tools and some specific debugging methods, this only involves web development.
General principles:
1. Find out the cause of the problem:
The program needs to be debugged because there is a problem with the program. The first goal of debugging is to find the cause. Common debugging methods, elimination method: When we face the entire complex system, we often have no idea where the problem lies; at this time, we can first eliminate the places that are not related to the problem, and gradually reduce the scope of debugging. Independent module method: Most programs have a module structure. Take out the suspicious module separately and simulate the corresponding operation data to see if there is a problem. Comparison method: If a program or system is based on a certain platform, you can first look at other programs or systems based on this platform to find out whether it is a platform problem or a problem with this program. Many times this method is used to rule out problems on immature platforms. hidden problems. Comparison method: For the programs you implement, you may already have corresponding open source modules. You can find the source codes of these programs and compare them with your own programs. This not only saves time, but also learns from the excellence of others.
2. Problem location:
There are many causes of problems. Maybe at this time you can only know what kind of problem it is and what it may be related to. At this time, you need to find out the code segment that appears. To achieve this step, the general development environment provides us with many practical tools. With the help of these tools, we can view the input and output of the program step by step, and locate problems based on the input and output of each step.
There are some very powerful people who can find the problem by reading the code. However, many problems are caused by the debugger's own negligence. It is almost impossible to find problems in this way because a fixed mindset has been formed.
3. Solve the problem
Find the cause of the problem and the corresponding line of code, and solving it is often a relatively simple matter, because these problems are often overlooked or poorly considered by oneself. But sometimes this is not always the case, but is caused by the external environment. For example, the number of visitors to your website increases. You did not consider the data pressure at the beginning, and you also did not consider the issue of high concurrency. At this time, You may find it more difficult to solve the problem. To solve such a problem, we must not only focus on the program, but also start from the system architecture, integrate all aspects of knowledge, and make comprehensive considerations.
Commonly used debugging tools for web development
As the saying goes: If a worker wants to sharpen his stone, he must first sharpen his tools. For program debugging, external tools must also be used. Here are several tools commonly used in developing web programs:
xdebug, xhprof: PHP performance debugging tool. They are all extensions of PHP and can be easily installed and configured. These two tools mainly tell you the internal running status of your program and the calling functions in a concise way. This allows you to see at a glance where the program is taking up too many resources and where optimization is needed. By the way, the profile files generated by xdebug are generally viewed with the help of winCacheGrid.
Firephp, firbug: The former is mainly a debugging tool for PHP, while the latter is an essential tool for web development. It can not only view requests, but also debug js and css.
HttpWatch can be used under ie. If you need to conduct in-depth analysis of the data, you can use packet capture tools.
Commonly used debugging functions in php: error_report, var_dump, print_r, var_export
Common debugging skills 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
Now I will introduce the specific debugging methods and techniques based on the program I debugged.
Example 1: This is a game engine developed in PHP. Its main function is to provide double-server player movement position, status, passing, skills and other functional controls. The entire program saves the results through an xml file. Suddenly one day, two users competed, and the returned xml did not conform to the normal structure. Judging from the returned results, the player's movement position was somewhat abnormal, and some steps were missing. So I decided to search through the game's AI, but I did not find anything abnormal here. Finally, the input player data is printed out, and then the data generated in the middle is also printed out. Combined with the processing logic of the program, it is found that the two users have the same player, which leads to confusion between the two players during processing. After finding the reason, just change the player logo to a decision made jointly by the team and the player.
Example 2: The program has just been launched, and some enthusiastic friends like to conduct some stress tests on the program. One time, a sudden 200,000 request for a large static resource directly caused the hard disk to crash. In this case, you have to look at the access logs to see what happened. Otherwise, if someone attacks you, you still won’t be able to find the reason. Of course, for such attacks, static resources should be placed directly on CDN. In addition, as the number of accessed users increases, the pressure on the system increases and the response gradually slows down. We have to consider: a program that used to only take 0.000 milliseconds to process now takes 0.0 seconds. At this time, the concurrency design of the program is very important. Otherwise, data anomalies may occur.
Example 3: Abnormality occurs when using external software: For example, when using memcached, if a certain machine does not start or cannot be used. At this time, nginx will usually return 502 to some requests using memcache. Doesn’t it feel a bit nonsensical? You can consider rewriting the php extension of memcahce