Skip to main content

Debug the PHP Application with Xdebug, WinCacheGrind and Windows

The Xdebug extension encourages you to troubleshoot your content by giving a great deal of profitable investigate data.

Installing Xdebug for XAMPP with PHP 7.x

Basic Requirements


  1. XAMPP for Windows: https://www.apachefriends.org/download.html
  2. The VC14 builds require to have the Visual C++ Redistributable for Visual Studio 2015 x86 or x64 installed
  3. The VC15 builds require to have the Visual C++ Redistributable for Visual Studio 2017 x64 or x86 installed

Setup

Download Xdebug for:

  1. PHP 7.0.x: https://xdebug.org/files/php_xdebug-2.5.5-7.0-vc14.dll
  2. PHP 7.1.x: https://xdebug.org/files/php_xdebug-2.5.5-7.1-vc14.dll
  3. PHP 7.2.x: https://xdebug.org/files/php_xdebug-2.6.0-7.2-vc15.dll
  4. Copy the file php_xdebug-2.6.0-7.2-vc15.dll to C:\xampp\php\ext
  5. Open the file C:\xampp\php\php.ini with Notepad++
  6. Disable output buffering: output_buffering = Off
  7. Scroll down to the [XDebug] section (or create it) and copy this lines

 
[XDebug]
zend_extension = "c:\xampp\php\ext\php_xdebug-2.6.0-7.2-vc15.dll"
xdebug.remote_autostart = 1
xdebug.profiler_append = 0
xdebug.profiler_enable = 0
xdebug.profiler_enable_trigger = 0
xdebug.profiler_output_dir = "c:\xampp\tmp"
;xdebug.profiler_output_name = "cachegrind.out.%t-%s"
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "127.0.0.1"
xdebug.remote_log = "c:\xampp\tmp\xdebug.txt"
xdebug.remote_port = 9000
xdebug.trace_output_dir = "c:\xampp\tmp"
;36000 = 10h
xdebug.remote_cookie_expire_time = 36000

     9. Restart the Apache (Stop/Start)

Now, if you run the phpinfo(); you can see the xdebug extension is enabled

phpinfo

By default Xdebug profile log name as cachegrind.out  You can add extra parameters to make dynamic. For myself, I added %t-%s which formats are

SpecifierMeaningExample FormatExample Filename
%ccrc32 of the current working directorycachegrind.out.%ccachegrind.out.1258863198.xt
%ppidcachegrind.out.%pcachegrind.out.5174.xt
%rrandom numbercachegrind.out.%rcachegrind.out.072db0.xt
%sscript name 2cachegrind.out.%scachegrind.out._home_httpd_html_test_xdebug_test_php
%ttimestamp (seconds)cachegrind.out.%tcachegrind.out.1179434742.xt
%utimestamp (microseconds)cachegrind.out.%ucachegrind.out.1179434749_642382.xt
%H$_SERVER['HTTP_HOST']cachegrind.out.%Hcachegrind.out.kossu.xt
%R$_SERVER['REQUEST_URI']cachegrind.out.%Rcachegrind.out._test_xdebug_test_php_var=1_var2=2.xt
%U$_SERVER['UNIQUE_ID'] 3cachegrind.out.%Ucachegrind.out.TRX4n38AAAEAAB9gBFkAAAAB.xt
%Ssession_id (from $_COOKIE if set)cachegrind.out.%Scachegrind.out.c70c1ec2375af58f74b390bbdd2a679d.xt
%%literal %cachegrind.out.%%cachegrind.out.%%.xt

Here %s is used for modifying only xdebug.profiler_output_name and not for xdebug.trace_output_name. 

And If you want to get the current profiling log file. you can call the function
xdebug_get_profiler_filename(), which returns the file name as a string


if you enable the xdebug.profiler_enable. it will run every single script. It will run all the time. So instead of that enable the xdebug.profiler_enable_trigger, the benefit of using profiler_enable_trigger you can enable and disable profiling by just pass the special GET or POST parameter XDEBUG_PROFILE to a PHP script. It will turn on profiling just for the one PHP script that receives the parameter. You need not set a value for XDEBUG_PROFILE, it is sufficient to append the parameter to the URL: yourUrl.php?XDEBUG_PROFILE.


Comments