Home > Backend Development > PHP Tutorial > How to debug PHP code? Summary of methods for debugging PHP code (recommended)

How to debug PHP code? Summary of methods for debugging PHP code (recommended)

不言
Release: 2023-04-03 18:04:01
Original
9656 people have browsed it

This article brings you how to debug PHP code? This summary of PHP code debugging methods has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

No programmer can complete a function or module in one go and flawlessly without debugging. Debugging actually involves many situations. In this article, I share my experience in actual development work. I personally understand that there are three types of debugging. Please note that what I am talking about is debugging and not testing

  • Functional debugging

  • Component debugging

  • Fragment debugging

Functional debugging refers to completing a Function or error, logic, and result testing during the process of completing the function

Component debugging refers to testing a plug-in or class that is being developed and tested for errors, logic, and results

Fragment debugging refers to testing the expectations and results of a piece of code, functions, and variables

Next let’s look at some of the debugging methods listed in this chapter

Violent debugging

This method is simple and crude, and is generally used by PHP programmers. It is browser debugging. After writing the code in the editor, open the universal browser and enter the address to start debugging the code.

Breakpoint debugging

To be honest, this method is completely unnecessary if you just look at the output results. It seems to be a waste of time and experience, but it is generally I always 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, which saves 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
Copy after login

If the code fragment is short, you can use

php -r "code ..."
Copy after login

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"
>>>
Copy after login

Download address: https://psysh.org/

test 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 are handed over to the 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.

理解需求 -> 分解需求 -> 整理逻辑 -> 设计代码 -> 敲代码 -> 调试 -> 测试
Copy after login

If you have never done the last step, then 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.

Editor debugging

I feel like talking about this is a bit nonsense. If you find it particularly difficult to open the command line and enter php xxxx, you can also You can choose to use the debugging that comes with the editor. Generally, sublime, visual Studio Code, and PHPStrom all have their own debugging tools. The principle is still to call php -r , but it is faster than opening the command line and entering commands. Much.

Related recommendations:

A brief introduction to abstract methods, abstract classes and interfaces in PHP

Non-forward calls and forward calls in PHP Code example

The above is the detailed content of How to debug PHP code? Summary of methods for debugging PHP code (recommended). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template