Debugging is a systematic process of spotting and fixing the number of bugs or defects in a piece of software so that the software is behaving as expected.
Useful Tools for Debugging in PHP Code:
What is Xdebug?
Xdebug is an extension for PHP to make life easier while debugging PHP application code.
Right now, you may be used to debugging your code with various other simple solutions. These include using echo statements at different states within your program to find out if application passes a condition or to get the value of a certain variable. Further, you might often use functions like var_dump, print_r or others to inspect objects and arrays.
What I often come across are, little helper functions, like this one for instance:
echo ‘<pre>'; var_dump($aVar); echo ‘</pre>'; print_r($var); exit;
What is required for using XDebug?
- Apache or IIS server
- XDebug extension for PHP
- IDE (ex: NetBeans, Eclipse, Visual Studio Code)
- XDebug IDE Plugin
Install Xdebug and Config:
First download xdebug extension suitable for your sever from https://xdebug.org/ website.
Then add entries for Xdebug in php.ini to look like this:
[xdebug] zend_extension="pathToDownloadedExtentionFile" xdebug.remote_enable=true xdebug.remote_autostart=true xdebug.remote_host=localhost xdebug.remote_port=9000
To start debugging we need a debugging client. Many IDEs support xdebugging through installing additional plugins. some of them are:
- Visual Studio Code
Once the plugin is installed you can start debugging your application code. To do that you need to configure the project in IDE. Here i explain how to configure xdebugging in NetBeans and Visual Studio Code.
In NetBeans, open the options window (Tools > Options) and go to the debugging tab for the PHP section. Enter the debugging port given in php.ini and a Session ID which you will need to pass with the requests you want to debug.
* I am assuming you have already installed NetBeans.
In Visual Studio Code:
In your project, go to the debugger and hit the little gear icon and choose PHP. A new launch configuration will be created for you with two configurations:
Listen for XDebug
This setting will simply start listening on the specified port (by default 9000) for XDebug. If you configured XDebug like I have recommended above, every time you make a request with a browser to your webserver or launch a CLI script, XDebug will connect and you can stop on breakpoints, exceptions etc.
Launch currently open script
This setting is an example of CLI debugging. It will launch the currently opened script as a CLI, show all stdout/stderr output in the debug console and end the debug session once the script exits.
* I am assuming you have already installed Visual Studio Code, PHP and XDebug plugin.
Profiling is the first step when optimizing any application. Profiling tools record important details like the time it takes for statements and functions to execute, the number of times they are called, and so on. The output can be analyzed to understand where the bottlenecks are.
Xdebug can also be used as a profiling tool for PHP. To start profiling your applications, add the following settings to php.ini:
xdebug.profiler_enable = 1 xdebug.profiler_output_name = xdebug.out.%t xdebug.profiler_output_dir = /tmp xdebug.profiler_enable_trigger = 1
Profiling is disabled by default in Xdebug, so xdebug.profiler_enable is used to enable it. xdebug.profiler_output_name is the filename of the profiler log (the %t specifier appends a timestamp to the filename; see the documentation for a full list of specifiers). Xdebug stores the profiling output in the directory specified by xdebug.profiler_output_dir. You can can change this to a location of your choice, but remember it must have write permissions for the user account under which the PHP script is run.
Profiling degrades performance since the PHP engine need to look after each function call and log its details, so you don’t want to run it all the time. xdebug.profiler_enable_trigger instructs Xdebug to perform profiling only when XDEBUG_PROFILE is passed as a GET or POST parameter.
- XDebug Documentions :
- Debugging in NetBeans: https://netbeans.org/kb/docs/php/debugging.html
- Debugging in Visual Studio Code:
- Debugging in PhpStom : https://confluence.jetbrains.com/display/PhpStorm/Debugging+PHP+Web+Applications+with+Run+Debug+Configurations