Home Backend Development PHP Tutorial Installation 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
echo php print var xdebug and use sharp weapon Install of programmer debug

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 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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles