search
HomeBackend DevelopmentPHP TutorialInstallation and use of PHP debugging tool XDebug_PHP tutorial

Installation and use of PHP debugging tool XDebug_PHP tutorial

Jul 14, 2016 am 10:10 AM
echophpprintvarxdebugandusesharp weaponInstallofprogrammerdebug

Many PHP programmers use echo, print_r(), var_dump(), printf(), etc. for debugging. Although these are enough for programmers with rich development experience, they can often perform debugging during program execution. In , by outputting the value of a specific variable, you can determine whether the program is executed correctly, and even the efficiency can be seen (of course, you may also need to use some time functions). So why do we need a special debugger to monitor the operation of our program?

In our usual PHP development, after a long period of accumulation of a large project, you will find that the performance is getting slower and slower, and where the performance is consumed is often a headache. Function a () has been called many times, and how much time has been consumed by function b(). How do we find out which bug is slowing down the running speed of our program? Here I would like to introduce to you a tool called xdebug. I believe many people have heard of it. I hope that with this tool we can simply analyze the performance bottleneck of PHP programs.

What is XDebug
XDebug is an open source PHP program debugger (i.e. a Debug tool) that can be used to track, debug and analyze the running status of PHP programs.

Install XDebug
Download php_xdebug.dll and download the one that is suitable for your operating system and PHP version according to the version number.
Place the downloaded php_xdebug.dll in the PHP installation directory phpext.
Edit php.ini. Some collection environments have their own xdebug configuration. If not, manually add the following lines:
1 [xdebug]

2 zend_extension = "/home/ad/php/lib/php/extensions/no-debug-non-zts-20060613/xdebug.so"

3 xdebug.auto_trace = on

4 xdebug.auto_profile = on

5 xdebug.collect_params = on

6 xdebug.collect_return = on

7 xdebug.profiler_enable = on

8 xdebug.trace_output_dir = "/home/ad/xdebug_log"

9 xdebug.profiler_output_dir = "/home/ad/xdebug_log"

Introduction to XDebug parameters:

zend_extension loads xdebug extension
xdebug.auto_trace automatically turns on function call monitoring
xdebug.auto_profile automatically turns on performance monitoring
xdebug.trace_output_dir sets the path to the output file of function call monitoring information.
xdebug.profiler_output_dir sets the path to the performance monitoring information output file.
xdebug.collect_params turns on the function of collecting "function parameters". Include the parameter values ​​of the function call in the monitoring information of the function procedure call.
xdebug.collect_return turns on the function of collecting "function return values". Include the return value of the function in the monitoring information of the function procedure call.
Restart Apache.
Write a test.php with the content of . If xdebug is seen in the output content, the installation and configuration are successful. Or go to /home/ad/xdebug_log to see if the log has come out.
Setting options
Category Setting Description

Log
xdebug.trace_output_dir
Log tracking output directory
xdebug.trace_output_name Log file name. xdebug provides a series of identifiers to generate file names in corresponding formats. For details, please refer to the official website
xdebug.trace_options How to add records to the file: 1 = Append (if the file exists). 0 (default) = Overwrite (if the file exists)
Display data xdebug.collect_params non-zero value = control function’s parameter display options

0 = Do not display.
1 = parameter type, value (for example: array(9)).
2 = Same as 1, but slightly different in CLI mode
3 = All variable contents
4 = All variable contents and variable names (for example: array(0 => 9)).

xdebug.collect_return 1 = Display function return value. Default 0 Do not display
xdebug.collect_vars 1 = Show which variables are used in the current scope and display the variable name. This option will not record the value of the variable. If necessary, use xdebug.collect_params
xdebug.collect_assignments 1 = Add a line to display variable assignments (if it is 1, it will be in the form $a = 1; this type of Assignment Expression will be displayed in the trace file)
Format xdebug.trace_format 0 = human readable. From left to right, each column represents: time point, memory, memory difference (needs to set xdebug.show_mem_delta=1), level, function name, function parameters (needs to set, xdebug.collect_params =1, as long as it is non-zero), the file name of the current code line, and the line number.
1 = machine readable[1]. Need to use third-party app, such as: xdebug trace file parser or xdebug trace viewer
2 = html format, that is, table, open with browser, display table

xdebug.show_mem_delta 1 = Show memory consumption (memory difference) of each function call
Behavior xdebug.auto_trace 1 = Turn on automatic tracing. (There are two tracing methods, one is automatic tracing, all php scripts will generate trace files when they run; the other is triggered tracing, as follows)
xdebug.trace_enable_trigger[2] 1 = Use XDEBUG_TRACE GET/POST to trigger tracing, or set the cookie XDEBUG_TRACE. In order to avoid generating the corresponding trace file for each request, you need to set auto_trace to 0

Note: This feature can only be set in version 2.2+

[xdebug-general] Re: Is trace_enable_trigger defunct?

Limit xdebug.var_display_max_depth array and object element display depth: mainly used in array nesting and object attribute nesting to display several levels of element content. Default 3.
xdebug.var_display_max_data How long to display when the variable value is a string. Default 512.
xdebug.var_display_max_children The number of array and object elements displayed. Default 128


Some custom functions
Function Description
void xdebug_enable() manually open, equivalent to xdebug.default_enable=on
void var_dump() overwrites the var_dump provided by PHP. When an error occurs, the function stack information is displayed (prerequisite: html_errors in php.ini is 1). Use xdebug.overload_var_dump to set whether to overwrite
void xdebug_start_trace(
string trace_file_path
[, integer options] ) Manually control the code segments that need to be traced
trace_file_path: file path (relative or absolute, if empty). If it is empty, or no parameters are passed, use the directory set by xdebug.trace_output_dir
options:

XDEBUG_TRACE_APPEND: 1 = append to the end of the file content, 0 = overwrite the file
XDEBUG_TRACE_COMPUTERIZED:
2 =Same as xdebug.trace_format=1 .
XDEBUG_TRACE_HTML: 4 = Output HTML table, the browser opens it as a table

void xdebug_stop_trace() stops tracing, code tracing stops at this line
string xdebug_get_tracefile_name() gets the output file name, used with xdebug.auto_trace.
void xdebug_var_dump([mixed var[,...]]) Output variable details, equivalent to var_dump in php, please see here for specific display
xdebug.show_local_vars defaults to 0 and is not displayed; when it is non-zero, when an error occurs in PHP execution, all local variables in the scope of the error code are displayed (Note: This will generate a lot of information, so the default is closed). The specific display difference is as follows [ 3]
array xdebug_get_declared_vars() displays the declared variables in the current scope
array xdebug_get_code_coverage() displays which lines of code are executed in a certain section of code [4]

Regarding xdebug.trace_format=1, if you use trigger mode to enable code tracing: (xdebug.auto_trace = 0;xdebug.trace_enable_trigger = 1), then you can add XDEBUG_TRACE to the URL, for example: localhost/test.php ?XDEBUG_TRACE, or localhost//test.php?XDEBUG_TRACE=1 (any value).

If you find it troublesome, then install a plug-in and let it help you. Chrome XDEBUG Helper, using it, you can switch to 3 states, disabled, debugging enabled, profiling enabled (detailed introduction in the next article), and then switch to debugging enabled. Run the script (remove ?XDEBUG_TRACE in the URL), and you can track the code.

Use xdebug_start_trace() and xdebug_stop_trace() to manually trace the execution of your code.

1 xdebug_start_trace();

2 //your code required to trace

3 xdebug_stop_trace();

Setting xdebug.auto_trace = 1 will enable automatic tracing before executing all PHP scripts. Alternatively, you can set xdebug.auto_trace = 0 through code and enable and disable tracing using the xdebug_start_trace() and xdebug_stop_trace() functions respectively. However, if xdebug.auto_trace is 1, tracing can be started before including the configured auto_prepend_file.

Options xdebug.trace_ouput_dir and xdebug.trace_output_name control where trace output is saved. Here, all files are saved to /tmp/traces, and each trace file starts with trace, followed by the name of the PHP script (%s) and the process ID (%p). All Xdebug trace files end with the .xt suffix.

By default, XDebug will display the time, memory usage, function name, and function call depth fields. If xdebug.trace_format is set to 0, the output will be human-readable (setting the parameter to 1 will be in a machine-readable format). Additionally, if you specify xdebug.show_mem_delta = 1, you can see whether memory usage is increasing or decreasing, and if you specify xdebug.collect_params = 4, you can see the types and values ​​of the parameters passed in. To monitor the value returned by each function, set xdebug.collect_return = 1.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477546.htmlTechArticleMany PHP programmers use echo, print_r(), var_dump(), printf(), etc. for debugging. This is enough for programmers with rich development experience. They can often program...
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software