Perform PHP Debugging By Integrating Xdebug With VsCode

Perform PHP Debugging By Integrating Xdebug With VsCode



When you decide to develop web applications, you must take care about few things for producing a smooth end product. A good developer always opts for best practices while developing robust apps. These practices include testing, debugging, performance and security of website or application. In the previous article, I had covered few topics from the above. In this article, I’ll discuss about PHP debugging concepts and techniques in PHP code.
Let’s start with the basic level, where you tend to write PHP code using print_r(), var_dump() commands to debug the output of the written code. These functions show you the correct output or errors / warnings in case of code failure. Although this PHP debugging technique is basic, but it is still in use.

Error Logs in PHP

When you are working in the dev environment, error reporting must be on so that you can have a quick look in browser what error logs in PHP are generating. You can enable / disable error logs from php.ini file and individual php files. The other best practice is to always create a configuration file in your project.
In php.ini file, you can enable it easily. Do remember that you must remove from each starting line.
  1. error_reporting = E_ALL & ~E_NOTICE
  2. error_reporting = E_ALL & ~E_NOTICE | E_STRICT
  3. error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ER… _ERROR
  4. error_reporting = E_ALL & ~E_NOTICE
In order to enable error logging in PHP for the current call in the individual file, you can write the following code on the top:
  1. ini_set('display_errors', 1);
  2. ini_set('display_startup_errors', 1);
  3. error_reporting(E_ALL);
But still you need to enable this line in php.ini file for reporting parse errors:
  1. display_errors = on
The above techniques is useful in any environment. But, if you have finally completed your app and are uploading to production servers, you might forget to revert the dev settings. In this case you can create a configuration file which includes the conditions in which you display and log errors. You can write the following code for error checking in PHP:
  1. define('DEBUG', true);
  2. error_reporting(E_ALL);
  3. if(DEBUG == true)
  4. {
  5. display_errors(true);
  6. log_errors(false);
  7. }
  8. else
  9. {
  10. display_errors(false);
  11. log_errors(true);
  12. }
You can define env in above method and can set your required conditions. Like, if you don’t want to log errors but just wants to display them in browser, you can define env in the above method for that.
Here are the different methods for error checking in PHP that you can use to debug php scripts, errors and warnings.
  1. // Turn off all error reporting
  2. error_reporting(0);
  3. // Report simple running errors
  4. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  5. // Reporting E_NOTICE can be good too (to report uninitialized
  6. // variables or catch variable name misspellings ...)
  7. error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
  8. // Report all errors except E_NOTICE
  9. error_reporting(E_ALL & ~E_NOTICE);
  10. // Report all PHP errors (see changelog)
  11. error_reporting(E_ALL);
  12. // Report all PHP errors
  13. error_reporting(-1);
  14. // Same as error_reporting(E_ALL);
  15. ini_set('error_reporting', E_ALL);

Debug Driven Development? What is this?

At the time of writing this article, I’m not sure if the debug driven development (DDD) term is right, but I got this idea from my friend who is always busy in debugging his code during the development phase. I have seen his code that contains multiple breakpoints and he roams through each line response till he gets the desired output :D. Don’t be confused about breakpoints. I’ll show you what are they. I thought this is a similar concept to TDD where you first write the test case, and then code to pass this test. DDD can have the similar flow where you will first define all the PHP debugging techniques for configuration, and then write the code to debug if any errors come up.
At this point, it is fair enough to introduce Xdebug to you all. It helps you understand the concept of PHP debug driven development. Basically, when you run PHP with Xdebug, it gives you full insights of errors, responses and code coverage.

What Exactly Xdebug is?

Imagine you have already written the code and now you are about to compile it. Bump! Something goes wrong and the code breaks. And worst still that you can’t identify what’s that. Xdebug is your saviour here. Basically, Xdebug is an extension of PHP which helps developers in debugging and smooth development of their projects to carefully watch for errors and solve them. It upgrades PHP’s var_dump() function. It also adds stack traces for Notices, Warnings, Errors and Exceptions.
Xdebug allows to add breakpoints and stop the code execution at every breakpoint, so that you can see variables output in only one iteration of code. Thus, saving your precious time which will  be otherwise lost in code debugging. Without Xdebug, you must refresh the page to see the output again.
You can also set breakpoints in dependent methods to see which one is causing issues. Also, you can add as many breakpoints as you want in the code, and then use PHP debugger to check each code line by line.

Install Xdebug in PHP

Now it’s time to install Xdebug in PHP. There are few options through which you can install Xdebug.

Windows:

For windows users, Xdebug is providing wizard tool through which you can download the related .dll file. For this, first create a PHP file in folder, and add the following line of code:
  1. <?php
  2. phpinfo();
  3. ?>
Run the file in browser. You will see the PHP information. Copy the details and put them in wizard tool and click on Analyze my phpinfo() output button.
This will give you the following output:
Now, download the .dll files and follow the above-mentioned instructions to put files in xampp/php/ext directory. Next, follow step 3 to enable Xdebug in php.ini file, and finally restart the webserver.

MAC

You can install PHP7 Xdebug using pecl extension:
  1. pecl install xdebug

Linux

On Linux distribution like Debian, you can use sudo to install Xdebug in PHP:
  1. sudo apt install php-xdebug;
Now, open the file etc/php/7.0/mods-available/xdebug.ini and add the following code to enable Xdebug:
  1. xdebug.profiler_enable_trigger = 1
  2. xdebug.profiler_enable = 0
  3. xdebug.remote_enable = 1
  4. xdebug.profiler_output_dir = "/tmp"
Now, restart the Apache2 server.
  1. sudo service apache2 restart;

GIT

You can also clone it from GitHub.
  1. git clone git://github.com/xdebug/xdebug.git
After installing Xdebug, open php.ini file and enable the extension.
  1. zend_extension="xdebug.so"
Now, check the PHP version using command `php -v`. You will see the Xdebug version installed with PHP.
  1. $ php -v
  2. PHP 7.2.0RC6 (cli) (built: Nov 23 2017 10:30:56) ( NTS DEBUG )
  3. Copyright (c) 1997-2017 The PHP Group
  4. Zend Engine v3.2.0-dev, Copyright (c) 1998-2017 Zend Technologies
  5. with Xdebug v2.6.0-dev, Copyright (c) 2002-2017, by Derick Rethans

Configure Xdebug in VSCode

If you want smooth workflow with Xdebug, you can integrate it in your favorite IDE like phpstorm, zend studio and VScode. Let’s configure Xdebug in VScode.
VSCode has a popular extension called php debug. You can find it in extension window and install it.
After installation, you must reload the VSCode window. Now, again run phpinfo(); method in any PHP file to check if Xdebug is enabled or not.
Now click on debug console tab and click on add configuration.
Now, you must select the environment which is PHP. VSCode will now add a launch.json file in the root directory.
Finally add the runtimeExecutable property to the list after port:
  1. "runtimeExecutable": "C:\\xampp\\php\\php.exe"
Save the launch.json file. Open debug mode tab, and click on green debug button to start debugging option.
You will now see few items in the window, through which you can select what logs Xdebugger will show like:
  • Notices
  • Warnings
  • Errors
  • Exceptions
  • Everything
Navigate to the Debug Console section which shows you the details of errors and the debug execution buttons on top.
You can select as per your choice. Now let me tell you the thing which I have already discussed in the early stage breakpoints. At this point, you can add breakpoints on the lines of code which you need to debug. Note that Xdebug will add the PHP debug script name with line number on the bottom left section:
You can run the application in browser, and then read the code line by line, to see the errors and debug them properly. Also, you need to remind few shortcut functions keys to move through functions and line of codes:
F5:  Continue Debugging
F10: Step Over
F11: Step into
Shift + F11: Step out

Xdebug Profiling

Xdebug also provides profiling of code just like other profiling tools, Blackfire and Tideways. If you want to use the profiling option, then you must enable it in php.ini file.
  1. xdebug.profiler_enable=1
  2. xdebug.profiler_output_dir="C:\xampp\tmp"
Now, open a file and start entering all the profiling logs.

PHP debugging Tools

Since PHP is a popular language, it offers a number of tools to work with different IDE and code editors. For debugging purposes, you can find the following tools:
  • Firephp
  • Sublime
  • PHPstorm
  • Netbeans
  • Zend studio
  • Kint
  • Webgrind
  • PHP_Dyn
  • PHP debug Bar
It’s not easy to decide which PHP debugger tool you should choose for checking error logs, because you must have basic knowledge about the tool’s functionality. As I’ve already discussed the integration of VScode, you can use it to your advantage.

Final Words

Debugging is always an important aspect of coding. Without it you can not see what is going wrong with the code. It’s a good practice to setup your debugging environment before writing the code, because, if you are developing complex application which contains thousands of lines of code, you will need a PHP debugger that finds errors and warning signs quickly.
In this article, you have learned how to enable PHP debugging with Xdebugger and configure it in VSCode. You can also integrate it in Eclipse, PHPstorm and other IDEs. If there is anything you want to know more about, do post your comments and suggestions in the comments section below.

Comments