Install and use XDebug, a PHP debugging tool. Many PHP programmers use echo, print_r (), var_dump (), and printf () for debugging. Although this is sufficient for programmers with rich development experience, they can often use echo, print_r (), var_dump (), and printf () for debugging in many PHP programmers, although this is sufficient for programmers with rich development experience, they can often determine whether the program is correctly executed by outputting the value of specific variables during program execution, even the efficiency can be seen (of course, some time functions may need to be used ). So why do we need a special debugging program to monitor our program running?
In our daily php development, after a large project has been accumulated for a long time, you will find that the performance is getting slower and slower, and what is the performance consumption, this is often a headache. how many times does function a () call, and how much time does function B () consume, how can we find out which worm slows down the running speed? Here we will introduce you to xdebug, a tool that many people have heard of. we hope to use this tool to analyze the performance bottleneck of php programs.
What is XDebug?
XDebug is an open-source PHP program debugger (a Debug tool) that can be used to track, Debug, and analyze the running status of PHP programs.
Install XDebug
Download php_xdebug.dll. download php_xdebug.dll based on the version number and your operating system and PHP version.
Put the downloaded php_xdebug.dll to the PHP installation directory php \ ext.
Edit php. ini. some collection environments already have xdebug configurations. 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"
XDebug parameters:
Zend_extension load xdebug extension
Xdebug. auto_trace automatically opens function call monitoring
Xdebug. auto_profile automatically enables performance monitoring
Xdebug. trace_output_dir: specifies the path of the output file of the function call monitoring information.
Xdebug. profiler_output_dir: set the path of the output file of performance monitoring information.
Xdebug. collect_params enables the function of collecting "function parameters. Include the parameter values of function calls in the monitoring information of function process calls.
Xdebug. collect_return enables the function to collect "function return values. Include the return value of a function in the monitoring information of function call.
Restart Apache.
Write a test. php file with the content: If xdebug is displayed in the output, the installation and configuration are successful. Or go to/home/ad/xdebug_log to check if the log has been generated.
Set Options
Category Setting Description
Logs
Xdebug. trace_output_dir
Log tracing output Directory
Xdebug. trace_output_name: log file name. xdebug provides a series of identifiers to generate file names in the corresponding format. for details, refer to the official website.
How to add the xdebug. trace_options record to a file: 1 = append (if the file exists). 0 (default) = overwrite (if the file exists)
Display data xdebug. collect_params non-zero value = control function parameter Display options
0 = not displayed.
1 = parameter type, value (for example, array (9 )).
2 = same as 1, but slightly different in CLI mode
3 = all variable content
4 = all variable content and variable names (for example, array (0 => 9 )).
Xdebug. collect_return 1 = display function return value. Default 0 is not displayed
Xdebug. collect_vars 1 = display the variables used in the current scope and the variable name. This option does not record the value of the variable. if necessary, use xdebug. collect_params
Xdebug. collect_assignments 1 = add a value for the display variable of a row (for example, $ a = 1; this type of Assignment Expression will be displayed in the trace file)
Format: xdebug. trace_format 0 = readable. each column from left to right indicates the time point, memory, and memory difference (xdebug needs to be set. show_mem_delta = 1), level, function name, function parameter (required, xdebug. collect_params = 1, as long as it is non-zero), the file name of the current code line, row number.
1 = Machine readable [1]. third-party apps are required, such as xdebug trace file parser or xdebug trace viewer.
2 = table in html format. open it in browser and display the table
Xdebug. show_mem_delta 1 = display memory consumption for each function call (memory difference)
Behavior xdebug. auto_trace 1 = enable automatic tracing. (There are two tracing methods. one is automatic tracing. trace files are generated when all php scripts run. The other is trigger tracing, as shown below)
Xdebug. trace_enable_trigger [2] 1 = Use XDEBUG_TRACE GET/POST to trigger tracing, or set cookie XDEBUG_TRACE. to avoid generating trace Tracing files for each request, you must set auto_trace to 0.
Note: This feature can be set only in version 2.2 +.
[Xdebug-general] Re: Is trace_enable_trigger defunct?
Restrict the xdebug. var_display_max_depth array and the display depth of object elements: mainly used in array nesting. When object attributes are nested, the display level of element content is. Default 3.
Xdebug. var_display_max_data variable value is the length of the string. Default 512.
Xdebug. var_display_max_children array and the number of display objects. Default 128
Some custom functions
Function Description
Void xdebug_enable () is opened manually, which is 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 segment to be traced
Trace_file_path: the file path (relative or absolute, if empty). if it is empty or no parameter is passed, use the directory set by xdebug. trace_output_dir
Options:
XDEBUG_TRACE_APPEND: 1 = End of the append object content, 0 = overwrite the object
XDEBUG_TRACE_COMPUTERIZED:
2 = same as xdebug. trace_format = 1.
XDEBUG_TRACE_HTML: 4 = output HTML table. open it as a table in the browser.
Void xdebug_stop_trace (): Stop Tracing. code tracing stops at this row.
String xdebug_get_tracefile_name () to obtain the output file name, used with xdebug. auto_trace.
Void xdebug_var_dump ([mixed var [,...]) output variable details, equivalent to var_dump in php. for details, see here
Xdebug. show_local_vars is 0 by default and is not displayed. if it is not zero, all local variables in the scope of the error code are displayed when an error occurs in php execution (note: This produces a lot of information, so the default value is closed ), specific display differences are shown in [3]
Array xdebug_get_declared_vars () displays the declared variables in the current scope
Array xdebug_get_code_coverage () shows the lines in a code segment [4]
About xdebug. trace_format = 1. if you use the trigger method to enable code tracing: (xdebug. auto_trace = 0; xdebug. trace_enable_trigger = 1), you can add XDEBUG_TRACE in the URL, for example: localhost/test. php? XDEBUG_TRACE, or localhost // test. php? XDEBUG_TRACE = 1 (any value ).
Is it very troublesome to install a plug-in to help you. Chrome XDEBUG Helper, you can switch to three states: disabled, debugging enabled, and profiling enabled (details in the next article), and switch to debugging enabled. Run the script (remove? XDEBUG_TRACE) to trace the code.
You can use xdebug_start_trace () and xdebug_stop_trace () to manually track your code execution.
1 xdebug_start_trace ();
2 // your code required to trace
3 xdebug_stop_trace ();
Set xdebug. auto_trace = 1 to enable automatic tracing before executing all PHP scripts. In addition, you can set xdebug. auto_trace to 0 in the code, and use the xdebug_start_trace () and xdebug_stop_trace () functions to enable and disable tracing respectively. However, if xdebug. auto_trace is 1, you can start the trail before including the configured auto_prepend_file.
Options xdebug. trace_ouput_dir and xdebug. trace_output_name are used to control the location where the trace output is saved. All files are saved to/tmp/traces, and each trace file starts with trace, followed by the PHP script name (% s) and process ID (% p ). All Xdebug trace files end with the. xt suffix.
By default, XDebug displays the time, memory usage, function name, and function call depth fields. If xdebug. trace_format is set to 0, the output conforms to human reading habits (set the parameter to 1 to machine readable format ). In addition, if xdebug. show_mem_delta = 1 is specified, you can check whether the memory usage is increasing or decreasing. if xdebug. collect_params = 4 is specified, you can view the type and value of the input parameter. To monitor the value returned by each function, set xdebug. collect_return = 1.
Examples (), var_dump (), printf (), etc. Although this is sufficient for programmers with rich development experience, they can often be in the program...