Preface
In daily development, var_dump() + die is often used for breakpoint debugging. Although it can meet business needs, it is not convenient enough. There are many shortcomings. For example, the breakpoint code affects the code structure. If the code has no comments, it will directly affect the code program. , if you delete the corresponding code and use it later, you need to repeat the operation, etc. It is recommended to use Xdebug to perfectly solve the above problems. The following is about how to use Xdebug under cli and using postman.
Install Xdebug
- Open https://xdebug.org/wizard and operate according to the following illustration.
Here it will provide expansion files with .dll / .so suffix according to your environment, PHP version and missing related extensions, and add extended statements according to the process. Just do it. - To verify the extension installation, enter php -m. If the following content is displayed, the extension is installed successfully.
If the following error occurs when php -m occurs in the Windows environment, just add zend_extension = xdebug.dll to the php.ini file without adding extension = xdebug.dll, and it will be solved.
PHP Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0
Warning: Xdebug MUST be loaded as a Zend extension in Unknown on line 0
PHP Warning: Module 'xdebug' already loaded in Unknown on line 0
Warning: Module 'xdebug' already loaded in Unknown on line 0
Related Xdebug configuration instructions
Relevant configuration description address , my configuration is as shown below
[Xdebug]
zend_extension = C:\xampp7\php\ext\php_xdebug.dll
;启用步骤调试。这可用于在代码运行时单步执行代码,并分析变量的值。
xdebug.mode=debug
;指定传递给DBGp调试器处理程序的IDE Key。
xdebug.idekey=PHPSTORM
;是否开启远程调试
xdebug.remote_enable = 1
;指定远程调试的处理协议
xdebug.remote_handler = dbgp
;可以设为req或jit,req表示脚本一开始运行就连接远程客户端,jit表示脚本出错时才连接远程客户端。
xdebug.remote_mode=req
;指定远程调试的主机名
xdebug.remote_host = localhost
;指定远程调试的端口号
xdebug.remote_port=9000
Configuration instructions for PHPStrom
-
Turn on Xdebug function
-
Define server configuration
-
Verify the debugger configuration on the web server
Implement Debug debugging in Cli environment
Reason: Because the PHPStorm script needs to select the execution file, and the Yii and Laravel frameworks need to execute commands such as php yii to execute, so breakpoints cannot be implemented, so we need to use some tricks.
- Create a php file that executes console commands as shown below
<?php
$command = 'php yii';
foreach ($argv AS $k => $v) {
if($k > 0) $command .= ' '.$v;
}
system($command);
-
Configure the running/debugging configuration as shown below. If you need to configure additional parameters, demo/test 0 1 will do.
-
break point
-
Run and view Debug and results, as shown in the figure below
WebAPI implements Debug debugging
Turn on PHP listening connection, as shown in the figure below
If you use the chrome browser, you can install the Xdebug helper plug-in and enable debug.
If you want to use tools such as postman, you can set Cookies, as shown in the figure below.
Related cookie parameters
XDEBUG_SESSION=XDEBUG_ECLIPSE; Path=/;
Tips: If you find something wrong, you can install Xdebug helper according to the chrome plug-in. After turning on debug, visit your domain name and check the corresponding Cookies information to know the result. Then follow the above process to see the breakpoint result.