Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Learn How to Debug PHP with Xdebug and VsCode

August 27, 2019

8 Min Read
PHP Debug
Reading Time: 8 minutes

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 debug 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.

Get 10x Faster Performance for Your PHP Applications

Optimize Your PHP Apps With Us & Get Faster Web Performance All The Time

Error Logs in PHP

When you are working in the dev environment on any web hosting for PHP, 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.

error_reporting = E_ALL & ~E_NOTICE

error_reporting = E_ALL & ~E_NOTICE | E_STRICT

error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ER… _ERROR

error_reporting = E_ALL & ~E_NOTICE

In order to enable PHP error logging for the current call in the individual file, you can write the following code on the top:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

But still you need to enable this line in php.ini file for reporting parse errors:

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:

define('DEBUG', true);

error_reporting(E_ALL);



if(DEBUG == true)

{

       display_errors(true);

       log_errors(false);

}

else

{

      display_errors(false);

       log_errors(true);

}

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.

Get Your PHP Deployment Ebook Now

Enter your email address below and get the download link.

Thank You

Your Ebook is on its Way to Your Inbox.

Here are the different methods for error checking in PHP that you can use to debug php scripts, errors and warnings.

// Turn off all error reporting

error_reporting(0);



// Report simple running errors

error_reporting(E_ERROR | E_WARNING | E_PARSE);



// Reporting E_NOTICE can be good too (to report uninitialized

// variables or catch variable name misspellings ...)

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);



// Report all errors except E_NOTICE

error_reporting(E_ALL & ~E_NOTICE);



// Report all PHP errors (see changelog)

error_reporting(E_ALL);



// Report all PHP errors

error_reporting(-1);



// Same as error_reporting(E_ALL);

ini_set('error_reporting', E_ALL);

PHP 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.

You Might Also Like: Learn Unit Testing in PHP With PHPUnit

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.

Xdebug is Pre-installed At Cloudways

At Cloudways you can easily Debug PHP with Xdebug. Enable Xdebug with Just Toggle button.

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:

<?php

phpinfo();

?>

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.

php xdebug

This will give you the following output:

php xdebug

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:

pecl install xdebug

Linux

On Linux distribution like Debian, you can use sudo to install Xdebug in PHP:

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:

xdebug.profiler_enable_trigger = 1

xdebug.profiler_enable = 0

xdebug.remote_enable = 1

xdebug.profiler_output_dir = "/tmp"

Now, restart the Apache2 server.

sudo service apache2 restart;

GIT

You can also clone it from GitHub.

git clone git://github.com/xdebug/xdebug.git

After installing Xdebug, open php.ini file and enable the extension.

zend_extension="xdebug.so"

Now, check the PHP version using command `php -v`. You will see the Xdebug version installed with PHP.

$ php -v

PHP 7.2.0RC6 (cli) (built: Nov 23 2017 10:30:56) ( NTS DEBUG )

Copyright (c) 1997-2017 The PHP Group

Zend Engine v3.2.0-dev, Copyright (c) 1998-2017 Zend Technologies

 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.

Start Growing with Cloudways PHP Hosting Today

People Love us because we never compromise on hosting

VSCode has a popular extension called php debug. You can find it in extension window and install it.

php debug extension

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.

add configuration

Now, you must select the environment which is PHP. VSCode will now add a launch.json file in the root directory.

launch file

Finally add the runtimeExecutable property to the list after port:

"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.

php debugging

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:

php debug script

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

php debug options

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.

xdebug.profiler_enable=1

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.

Q: How to debug PHP in Chrome?

A: You can easily debug PHP in Chrome using a simple extension called PHP Console. Just install this PHP debugging tool from the Chrome web store and start logging errors, warnings, exceptions and vars dumps on your Chrome browser.

Q: How to debug PHP in Firefox?

A: To easily debug PHP applications in Firefox, you can use the PHP debugger extension named Xdebug Helper for Firefox. This Xdebug PHP extension helps developers in profiling and tracing PHP codes fast on Mozilla Firefox. It also helps you to enable/disable debugging easily instead of tedious POST/GET variables & cookies.

Q: How to disable Xdebug in PHP?

A: To disable Xdebug in PHP, go to your php.ini file and find an entry with Xdebug. Comment it out by putting “;” to the extension as:
; zend_extension = “/path/to/php_xdebug.dll”
Restart your apache server and you will see Xdebug disabled in the next stage.

Q: How to resolve PHP debugger not installed error?

A: To resolve this error, check the file in your C:/php/ext/ directory, which may contain some missing or wrong extension. 

Follow these five steps to resolve this error:

  1. Go to xDebug
  2. Download Xdebug 2.4.0rc1
  3. Rename it to php_xdebug.dll
  4. Paste this file in “php/ext” folder
  5. Restart your server.

Q: What are the PHP debugging tools for Firefox?

A: You can debug PHP code in Firefox using multiple debugging tools. Firefox provides extension for popular PHP debugging tools including FirePHP, Xdebug, Firebug and others.

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 debug with Xdebug 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.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch PHP websites without the worry of Server Management.

Pre-Installed Optimized Stack with Git, Composer & SSH

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. Besides his work life, he loves movies and travelling. You can email him at shahroze.nawaz@cloudways.com

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!