Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the debugging methods in PHP

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what are the debugging methods in PHP". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what are the debugging methods in PHP" can help you solve the problem.

The debug (debugging) methods in PHP are as follows: 1, add echo, var_dump, print_r and exit statements to the PHP code, print information through the browser for debugging; 2, use Xdebug for debugging; 3, debug through the console terminal.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Debug (debugging) method commonly used in PHP

Print information through the browser for debugging

Method

Add echo, var_dump, print_r, and exit to the code, and view the output in the browser.

Advantages and disadvantages

Advantages:

Simple, easy to use, no need to install plug-ins

For your own code, or a familiar framework, you can use it like this

Disadvantages:

For multi-branch logic, you need to add a lot of code or try many times

For unfamiliar logic, the complete execution process cannot be reflected.

It is possible to leave debug statements in the project.

Unable to step in

Sports Acrobatics

When debugging, in order to format the output variables, you often need to implement your own dump () function in the project. With Composer, you can globally install the dump () function in the symfony/var-dumper package so that all projects can be used without changing the project.

Install the symfony/var-dumper package globally:

It will be installed to the ${HOME} / .config/composer directory by default

Composer global require symfony/var-dumper

Modify the php.ini file to include the specified file before executing the PHP code

Auto_prepend_file = ${HOME} / .config/composer/vendor/autoload.php

Debugging with Xdebug

XDebug is the XDebug S structure, where Client is the Xdebug,Server installed in PHP and the plug-in installed in IDE, which uses the DBGP protocol to communicate. When PHP runs the script, it sends debugging information to IDE through the Xdebug plug-in and receives the control signal sent by IDE.

You need to install and open Xdebug for PHP, and then set up the Xdebug plug-in for IDE so that the two can communicate.

Advantages and disadvantages

Support single-step debugging and the acquisition of arbitrary variable values

Complex configuration requires IDE to install the plug-in

Support the cooperation with the browser, and the XDEBUG_SESSION_START parameter needs to be included in the request.

Web App debugging

For web applications, to turn on Xdebug debug mode, additional flags must be added to requests sent by the browser. You can add XDEBUG_SESSION_START=session_name to the GET/POST/Cookie parameter so that Xdebug understands that the request needs to be debugged to connect to the IDE.

However, it is also troublesome to set up manually each time, and there are two ways to simplify the operation:

Use the method provided by IDE. For PhpStorm, refer to Debugging PHP Web Applications with Run Debug Configurations. When you use it, you need to configure Web Server in IDE, then set up a PHP Web Application, click the Debug button to start debugging, and then IDE will automatically open the browser and enter the URL, and add XDEBUG_SESSION_START=session_name.

Using the browser plug-in, after turning on the plug-in's debug switch, the plug-in can automatically bring the corresponding Cookie to the request. Xdebug helper can be installed for Chrome.

Debug through console terminal (CLI mode)

For non-web applications, such as scheduled tasks or unit testing, you can debug directly in the console.

Open the command line terminal through the Alt+F12 shortcut key in PhpStorm. However, because only one terminal can be displayed in IDE, the debug terminal will overwrite the command line terminal after debugging is enabled, so open a separate command line terminal (you can use DOS window or PowerShell under Windows).

Method and principle

Web applications use GET/POST/Cookie parameters to mark debug requests, while non-web applications enable debugging by setting environment variables on the command line terminal.

Two steps:

Set the environment variable XDEBUG_CONFIG= "idekey=session_name", which needs to be the same as the idekey set in the Xdebug section of the php.ini.

Execute the script at the command line terminal. The debug terminal of IDE will be aroused during execution, which can be debugged step by step, and the output result will be displayed on the command line terminal in real time.

IDE usually provides quick operation, and for PHPStorm, you can refer to Debugging PHP CLI scripts with PhpStorm.

After debugging is started through IDE, IDE starts the Xdebug plug-in to listen on a port (PhpStorm defaults to 9000, but this conflicts with PHP-FPM and can be changed to 9001) to obtain the debugging information returned by the PHP server.

D:\ lnmp\ php72\ php.exe-dxdebug.remote_enable=1-dxdebug.remote_mode=req-dxdebug.remote_port=9001-dxdebug.remote_host=127.0.0.1 D:\ lihongfeng\ workspace\ untitled\ index.php set, view and release environment variables

Linux

Export XDEBUG_CONFIG= "idekey=session_name" / / set the environment variable echo $XDEBUG_CONFIG / / View the environment variable unset XDEBUG_CONFIG / / delete the environment variable

Windows

Set XDEBUG_CONFIG= "idekey=session_name" / / set the environment variable echo% XDEBUG_CONFIG% / / View the environment variable set XDEBUG_CONFIG / / View the environment variable set XDEBUG_CONFIG= / / Delete the environment variable about "what are the debugging methods in PHP". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report