Home  >  Article  >  Backend Development  >  how to test php code

how to test php code

(*-*)浩
(*-*)浩Original
2019-09-09 13:53:084451browse

No programmer can complete a function or module in one go and flawlessly without debugging. Debugging actually involves many situations.

how to test php code

Violent debugging

This method is simple and crude. Most PHP programmers will use it. That is browser debugging. When editing After writing the code in the browser, open the universal browser and enter the address to start debugging the code.

Breakpoint debugging (Recommended learning: PHP programming from entry to proficiency)

To be honest, if you just look at this method Outputting results is completely unnecessary. It seems like a high-level waste of time but is actually a waste of time. Generally, I will run debug after the code block is written to check whether the process and results are within my expectations or if I encounter some logical problems that I cannot detect. It is more reliable to choose breakpoint debugging, saving time and effort. Even the most powerful programmers have moments of confusion, right?

Command debugging

If you download a code snippet from the official website or github and are eager to debug it but are not willing to put the code snippet into the project or open a new one Directory, you can use the php command to execute this code block

php -f filename.php

If the code fragment is short, you can use

php -r "code ..."

to execute and run the PHP code

Fragment debugging

When you suddenly think of a method or algorithm and are eager to verify it, but are unwilling to open the editor or execute the command line that comes with PHP. You can choose to use the psysh tool to execute and verify your ideas. The installation of this tool is very simple

Psy Shell v0.8.17 (PHP 7.1.14 — cli) by Justin Hileman
New version is available (current: v0.8.17, latest: v0.9.6)
>>> date('Y-m-d H:i:s',time())
=> "2018-08-09 14:18:10"
>>>

Download address: https://psysh.org/

Testing and debugging

This is a serious debugging method. Many programmers think that the task ends after the functional modules and components are developed, and then they can be handed over to testers for testing. In fact, this is not the case. Some hidden bugs or fatal errors, logic errors, and system errors (such as starting useless threads, unused variables, methods, classes, etc.) cannot be tested. In fact, as a programmer, the process of developing functions is like this.

Understand the requirements-> Decompose the requirements-> Organize the logic-> Design the code-> Type the code-> Debugging-> Test

If you never do the last step If you haven't done it, you are only half a programmer. For testing, I recommend using PHPunit. Only by better understanding your own code can you be a qualified programmer.

The above is the detailed content of how to test php code. For more information, please follow other related articles on the PHP Chinese website!

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