Home > Article > Backend Development > Use xdebug to debug PHP to find the bottleneck of the PHP program_PHP tutorial
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:
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.
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.
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
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.
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
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.
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.
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!