This article brings you to the content of how to debug PHP code? PHP code Debugging Method Summary, there is a certain reference value, the need for friends can refer to, I hope to help you.
No programmer can one go and flawlessly complete a function or module without debugging. Debugging actually has many different situations. This article I share my own experience in the actual development work, I personally understand that debugging in three kinds, note that I am talking about debugging is not a test
Functional commissioning
Component debugging
Fragment debugging
Functional debugging refers to the testing of errors, logic, and results in the process of completing a function or completing a function.
Component debugging refers to testing a plug-in, a class for errors, logic, and results that are being developed for testing
Fragment debugging refers to testing a piece of code, a function, a variable's expectations, and a result
Now let's look at some of the debugging methods listed in this chapter
Violent commissioning
This method is simple and rough, the general PHP programmer will use, that is, browser debugging, after writing the code in the editor and then open the Universal Browser input address to start debugging code.
Breakpoint Debugging
To tell the truth, this way if only to look at the output, completely unnecessary, seemingly tall on the actual waste of time experience, generally I will write in the code block after debugging, check whether the process and results in my expected or encountered some logic problems can not be detected, or choose Breakpoints Debugging more reliable, Save time and effort. The most powerful programmers have a moment, don't they?
Command debugging
If you download a snippet of code on your website or github, and you're eager to debug but don't want to put snippets into your project or create a new directory, you can use PHP commands to execute this block of code.
Php-f filename.php
If the code snippet is short, you can use
Php-r "Code ..."
Run PHP code execution
Fragment debugging
When you suddenly think of a method or algorithm eager to verify him, but do not want to open the editor or execute the command line PHP comes with. You can choose to use the psysh
tool to perform verification of your ideas, this tool is very easy to install
Psy Shell v0.8.17 (PHP 7.1.14-cli) by Justin hilemannew version is available (current:v0.8.17, latest:v0.9.6) >>& Gt Date (' y-m-d h:i:s ', Time ()) = "2018-08-09 14:18:10" >>>
Download Address: https://psysh.org/
Test debugging
This is a serious debugging method, many programmers think the function module, component development end of the task, and then to the testers to test it. In fact, there are hidden bugs or fatal errors, logical errors, system errors (such as the opening of useless threads, unused variables, methods, classes, etc.) that cannot be tested. Actually as a programmer, the process of developing the functionality is this way.
testing, debugging, design code, logic---decomposition requirements, demand--understanding requirements
If the last step you have never done, then you can only be half a programmer. Test I recommend the use of PHPUnit, better to understand their own code, is a qualified programmer.
Editor debugging
I feel like talking about this a bit of nonsense, if you feel open the command line and then input php xxxx
particularly laborious, you can also choose to use the editor's own debugging, generally such as Sublime,visual Studio Code, Phpstrom will bring the debugging tools, the principle is still called php -r
but it's much faster than opening a command line to enter a command.