Home > Backend Development > PHP Tutorial > Use xdebug to debug PHP to find the bottleneck of the PHP program_PHP tutorial

Use xdebug to debug PHP to find the bottleneck of the PHP program_PHP tutorial

WBOY
Release: 2016-07-21 14:53:57
Original
998 people have browsed it

xdebug is an extension application of PHP. With xdebug, you can easily debug PHP programs. With it, we don’t have to use fools like <font style="BACKGROUND-COLOR: #eaeaea" face="NSimsun">var_dump()</font> and <font style="BACKGROUND-COLOR: #eaeaea" face="NSimsun">print_r()</font> Debugging function.
Xdebug provides more functions and can debug PHP scripts in all aspects. XdebugThe main functions are as follows:

  1. Stack trace and function trace;
  2. Error message;
  3. Memory allocation;
  4. Analyze php script information;
  5. Code coverage analysis;
  6. Remote debugging analysis;

1. Set up xdebug debugging environment on Ubuntu

Install Xdebug module

Use apt-get to install xdebug in the terminal, the command is as follows

sudo apt-get install php5-dev php-pear
sudo apt-get install php5-xdebug

Then open the php.ini file (eg gedit /etc/php5/apache2/php.ini). Configure as follows;

;Load xdebug dynamic link library
extension=xdebug.so
;xdebug analysis file output path
xdebug.profiler_output_dir = “/var/www/xdebug/”
;The analyzer default is Turn it off. When debugging, just add XDEBUG_PROFILE=true to the url
xdebug.profiler_enable = Off
xdebug.profiler_enable_trigger = 1

Restart apache so that you can use xdebug to debug PHP.

2. Install xdebug under windows

Take PHP5.1.4, Windows platform as an example (for other PHP versions, please refer to the official website documentation for other platforms):
1. Log in to www.xdebug.org, there is a Windows modules on the right side of the homepage, select PHP5.1.2+ among them, download the php_xdebug-5.1.2-2.0.0beta6.dll file;
2. Place the downloaded php_xdebug-5.1.2-2.0.0beta6.dll into the C:php5ext directory and rename it to php_xdebug.dll;
3. Edit php.ini and add the following lines:

extension=php_xdebug.dll
[Xdebug]
xdebug.profiler_enable=on
xdebug.trace_output_dir=”I:Projectsxdebug”
xdebug.profiler_output_dir=”I:Projectsxdebug”
xdebug .dump.GET=*
xdebug.show_local_vars=1

For specific parameter PHP.ini description, please refer to the comments under ubuntu.

3. Use xdebug for PHP debugging

xdebug debugging variables are more friendly

Xdebug rewrites the <font style="BACKGROUND-COLOR: #eaeaea" face="NSimsun">var_dump()</font> function in php.
Var_dump() in xdebug gives variable objects different colors, displays the type length, and can also control the display level. The display method is formatted, clear and friendly.
To use this function, you need to pay attention to the following parameters.

;Whether to override the function var_dump() in PHP; the default is on, the value is 1; set to 0, it is off;
xdebug.overload_var_dump = 1
;Control the size of the array sub-element display by default 256
xdebug.var_display_max_children = 256
; Controls the size of variable printing, the default is 512
xdebug.var_display_max_data = 512
; Controls the level of array and object element display. Default is 3
xdebug.var_display_max_depth = 3

xdebug’s stack tracing function

If there are incorrect function parameters, repeated methods, syntax errors and other errors in the script. xdebug can trace the process of its error generation.

Please refer to http://xdebug.com/docs/stack_trace for parameter configuration; generally speaking, configuration is rarely required.

4. Use xdebug for script performance testing

Make the following configuration in php.ini

;The default is 0, the xdebug debugger is not enabled;
xdebug.profiler_enable = 0
;The default is 0; here set to 1, you can pass the XDEBUG_PROFILE parameter through GET/POST
xdebug.profiler_enable_trigger = 1

Test performance results viewing tool

After the test is completed, we need to check the test results to find the bottleneck of PHP.
Two viewing tools are recommended here, KCachegrind can be used under ubuntu, and WinCacheGrind can be used under windows. To check specifically on how to use the xdebug test result tool, you can Google it. It is very simple. You can understand it if you know a little bit about E-text.

5. Notes on using xdebug and related parameter descriptions

Notes on using xdebug

If you don't see the Xdebug section in the output of phpinfo(), Xdebug loading failed. The Apache error log lists the reason. Common errors include the wrong path to zend_extension or conflicts with other extensions. For example, if you need to use XCache and Xdebug, be sure to load XCache first. However, since Xdebug is intended for use during development and assumes the path to xdebug.so is correct, you will need to disable the other extensions and try again. You can then re-enable the extension to perform additional tests, such as the effects of caching.

The Xdebug site has some other troubleshooting tips.

xdebug related parameter settings

xdebug.default_enable

Type: Boolean Default value: On
If this is set to On, stack traces will be displayed in error events by default. You can disable the display of stack traces by using xdebug_disable() in your code. Because this is one of the basic functions of xdebug, it is wise to set this parameter to On.

For exciting content, please click on the next page!

  • Total 2 pages:
  • Previous page
  • 1
  • 2
  • Next page

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364700.htmlTechArticlexdebug is an extension application of PHP. With xdebug, you can easily debug PHP programs. With it, we There is no need to use fool debugging functions such as var_dump() and print_r(). Xd...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template