How to debug PHP?

Source: Internet
Author: User
Tags mysql manual fast web

Debugging is the simplest (right ?) :
It was okay to install PHP, But I copied someCodeTo find out that it cannot be used, you can use your eyes to carefully correct the code in the book. You must have copied the wrong copy or printed the wrong copy.
For example, 1 (yi), L (EL), 0 (Ling), and O (capital O ).

Don't laugh. Someone copied it and missed it. I used to help debug it. I entered it (but he didn't save it). That's all right.

Back to business
Q: "Why debugging? "
A: Of course, this is becauseProgramWrong. What else do you think I have?
Q: "I think my program is correct! "
A: The expected results cannot come out. Of course, errors occur. People with such ideas do not have the idea of debugging, but are not capable of debugging.
Q: What is the purpose of debugging?
A: No matter whether the PHP you configured is wrong, whether your program is wrong, or whether the program you wrote is correct or the PHP (such as free space) configured by others, you can find out the cause by learning to debug it.
Q: Can I debug logic disorder?
A: Maybe you have changed it occasionally and thought it was debugging. In fact, it is not just debugging, but you have repeatedly used the program code to think about it, and repeatedly modify the code to "practice" whether an idea is feasible.
It can be said that it is "debugging logic" rather than "debugging code ":
There is no code or code disorder in the logic -- debug/modify the logic --> the correct logic-> reflected in the Code and the correct code.
The simple debugging code is:
Correct logic-encoding-> error code-debugging-> correct code
Therefore, debugging can be divided:
1. debug logic, 2. debug code, 3. debug interface. 4. Etc ..
Wrong logic is not the right program. To write a program, you must first clarify the logic (process) before coding.
The merged items are:
Vague logic -- debugging logic --> correct logic -- encoding --> wrong code -- debugging code --> correct auma
In the debugging logic, you can use "modify code" to help avoid exhausting your mind, but your mind must be dynamic and cannot be changed without thinking about it, and cannot be mixed with "Debug code.
Small errors are often solved by mixing them together, but we need to develop the habit of separation, so that we can easily solve large errors.

Don't panic
When a beginner encounters a mistake, he becomes panic. He only knows in his mind that "no, no, it's a terrible mistake. Ask someone." calm down and study based on what he learned. What is debug, how to debug, what should I do if an error occurs?

Basic Debugging:
1. Enable the debugging function: Set error_reporting = e_all and display_errors = on in PHP. ini to restart the Web Service (APACHE)
2. Refresh the error page to view the name of the row with the error prompt
3. open the file and locate the error line. For example, the Code echo $ ABC [2];
4. incorrect understanding:
A. Check the manual to understand the meaning of errors. First, you must understand the language. For example, the simplest undefined index 2 means that the array does not exist. This indicates that you have accessed an element that does not exist in an array.
B. if you already know how to change it, modify it directly, for example, Echo $ ABC [0].
C. If you don't know, add var_dump ($ ABC) to the variable content in the same place. Refresh the page to see what elements are contained in $ ABC.
D. If you think $ ABC [2] should exist, search for the error source, go back to the source, or use var_dump (debug_backtrace ());
Var_dump ($ _ post, $ _ Get, $ _ cookie, $ _ server...) when necessary ....)

I have not used single-step debugging (step by step). If the debugger is relatively simple, I can pause it to see if the variable content is a value that should appear in the end. If not, where does it generate this value?

Pay attention to the environment
Environment:
Why are you having a problem when so many people around the world are okay? Do you fall in love with you or do you fall in love with you?
In fact, your "environment" raises your bug.
Read books/manuals at ordinary times and record what is "environment ".
$ _ Get $ _ post $ _ cookie $ _ session $ _ server these are crucial factors for running the program. Of course, you can also var_dump your own $ globals to check the content.
There are also strtolower/strtoupper functions related to setlocale (), while the default value is related to getenv ("Lang") getenv ("lc_all") in Linux (putenv does not necessarily work)
There are other configurations such as PHP. ini.
There are also web server configurations. For example, Apache does not support some functions on other servers.

Btw1: Your debugging capability is complementary to your encoding capability. Based on your coding experience, what else do you think you should pay attention?
Btw2

 

-------------------------------- The following content is relatively cool
You can solve the problem at a glance, but if you have no clue, refer:
Debugging direction:
Catch!
A. Capture the thief first, and the biggest mistake is logical errors. Review the logic first, and then the key part of the Code (which may be related to errors)
B. Peat the root of the root. If there is a cause, you will know the "result" of the final error and the "cause" of the error will happen after you touch it. For example:
If ($ logined ){
$ A = 100;
}
$ _ Session ['a'] = $ A; the prompt "undefined variable $ A" is displayed. Obviously, you need to find the place where the $ a variable is generated, locate the if block and find that $ A is available only when if true.
If ($ logined ){
$ A = 100;
} Else {
$ A = 0;
}

For example, there are many prompts when a function error occurs in MySQL.
Prompt Password error: Check whether the parameter password is correct or the password set on the MySQL server is wrong.
Prompt that the SQL field does not exist. Open phpMyAdmin to go to the table and check if any of the field programs are wrong, forget to add the table, or make a mistake.
A statement error is prompted. Find the MySQL Manual instead of the PHP manual.

Split!
What is the so-called "One Mind"? But we are a huge code that one person faces, so we need to come up with "vice versa" and separate errors from the code, separate multiple errors. as a result, it is only a small number of errors for you to eliminate one by one.

Recall:
Recall whether a similar error prompt was encountered before and how the problem was solved at that time. If you did not recall it, you should find a new method.
For beginners, repeated exercises are very important. Memories can strengthen the abilities/methods they have learned until they are integrated and can be applied freely.

Comparison:
Similar to what is mentioned at the beginning of this article, it is to find the correct code for comparison.
Sometimes there is no correct code. In this case, you can re-develop a correct code in your mind to compare it, which is a little difficult, but this is to avoid thinking based on the wrong idea.
Then, let's look at the manual and follow the instructions in the manual to find out what should happen.

oo idea
requires your program to be Oo (not necessarily an object, but OO in terms of structure/idea)
OO programming is simply an object, each type of object can complete a function, just like a mouse, and a single object/or part of code can work normally only with the desired data.
the OO debug method requires the idea that one of my objects (or code) only needs some data (in the environment) it will work normally
If
1. if there is a normal data error, it indicates that this object is designed incorrectly
2. if the incoming data is abnormal, it indicates that the code for using this object is called incorrectly
experts may think this article is "silly". In fact, for beginners (including me) this idea should be formed and the idea should be used for debugging all the time, but it will take a long time to get close to the master to understand it.
it is a bit like the 'detaching 'method, but the difference is that when debugging:
1. you can debug an object independently. Because a good object (or code) only requires as simple data as possible, complex data may survive inside or outside the object, instead of cross-outbound/inbound objects (that is, objects that are not passed in or transferred out)
2. for fast web debugging, you do not have to extract all the code of the object. You just need to call the part of the object and add some statements to view the object, such as var_dump ($ OBJ) var_dump ($ obj-> myabc);
or ECHO $ http-> download:
$ http-> seturl ("http: // localhost/abc.html");
This object works normally, now you can open the code of this object to see if there are any other errors.

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.