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.

Peak Performance.

Limitless Scalability.

  • 0

    Days

  • 0

    Hours

  • 0

    Mins

  • 0

    Sec

Off For 4 months
+40 free Migrations

Secure Any CMS With Ease with Our Malware Protection Add-On! LEARN MORE→

How to Set Up PHP Debugging Using Xdebug and VSCode

Updated on December 1, 2024

9 Min Read
debug-php

You must pay attention to a few things when developing web applications to deliver a smooth end product. Good developers follow the development best practices, including testing, debugging, etc., while creating robust apps.

This article discusses the PHP debug concepts and coding techniques. So read on to learn more about debugging PHP.

You can start debugging without any configuration. But, this is only suggested when you need to connect to an existing Web Server with Xdebug configured or want to run and debug a PHP script rapidly.

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. We have many PHP debugging tools, that let user urges to fix the errors during coding, and some functions that show you the correct output or errors/warnings in case of code failure. Although this PHP debugging technique is basic, it is still in use.

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

Error Logs in PHP

When you are working in a dev environment on any web hosting for PHP, you must turn on error reporting to check if the PHP error logs are being generated. You can enable/disable error logs from php.ini and individual php files.

The other best practice is to create a configuration file in your project. And you can enable it in the php.ini file. But remember to remove the semicolon (;) from each 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

To enable PHP error logging for the current call in the individual file, insert the following code :

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

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

display_errors = on

The above techniques are useful in any environment. You may sometimes forget to revert the dev settings when you have finished your app and uploaded it to production servers.

In this case, you can create a configuration file that 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 the above method and can set your required conditions. For example, when you don’t want to log errors but just want to display them in the browser.

Host PHP Websites with Ease [Starts at $11 Credit]

  • Free Staging
  • Free backup
  • PHP 8.0
  • Unlimited Websites

TRY NOW

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);

What is PHP Debug Driven Development?

Earlier, I was unsure of the debug-driven development (DDD) term, but I got this idea from a debugger friend. His code contained multiple breakpoints, and he went through each line response till he got the desired output.
Don’t be confused with breakpoints, as I will demonstrate them. DDD has a similar flow to TDD, where you first define all the PHP debugging techniques for configuration and then write the code to debug if any errors arise.
Once you have an idea of DDD, you should learn about Xdebug, which helps you understand the concept of PHP debug-driven development. Running PHP with Xdebug gives you full insights into errors, responses, and code coverage.

What Is Xdebug?

Imagine you’ve written your code, and the code breaks just when you compile it. And the worst part is that you can’t identify what went wrong. Xdebug is your savior here.

Xdebug is a PHP extension that helps developers debug and smoothen the development of their projects to watch for errors and resolve them. It upgrades PHP’s var_dump() function and adds stack traces for notices, warnings, errors, and exceptions.

Xdebug uses the DBGp debugging protocol and gives debugging and profiling capabilities. It allows us to add breakpoints and stop the code execution at every breakpoint to see variables output in only one code iteration.

So Xdebug lets you save your time, which is otherwise lost in code debugging. Without Xdebug, you must refresh the page to see the output again. You can also set breakpoints independent methods to see which one is causing issues.

Also, you can add as many breakpoints as you want in the code and use a PHP debugger to check each code line by line.

Install Xdebug in PHP

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

Windows:

For windows users, Xdebug is providing a 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 the browser. You will see the PHP information. Copy the details and put them in the 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 the xampp/php/ext directory. Next, follow step 3 to enable Xdebug in the 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 the php.ini file, and enable the extension.

zend_extension="xdebug.so"

Now, check the PHP version using the 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 a smooth workflow with Xdebug, you can integrate it into your favorite IDE like phpstorm, Zend studio, and VScode. Let’s configure Xdebug in Visual Studio Code to debug PHP.

Improve Your PHP App Speed by 300%

Cloudways offers you dedicated servers with SSD storage, custom performance, an optimized stack, and more for 300% faster load times.

There is a popular VSCode PHP extension called php debug. You can find it in the 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 the 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 the debug mode tab, and click on the green debug button to start the 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.

At this point, you can add breakpoints on the lines of code that you need to debug. Note that Xdebug will add the PHP debug script name with the line number on the bottom left section:

php debug script

You can run the application in the browser, and then read the code line by line, to see the errors and debug them properly. Also, you need to remind a 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 the 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.

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 dump 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 the PHP debugger not installed error?

A: To resolve he PHP debugger not installed 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.

Revolutionize Your Website Experience. Join Cloudways today!

Step into the future of hosting with Cloudways Managed hosting. Experience seamless scalability, unmatched speed, and ironclad security.

Final Words

Debugging is an important aspect of coding; without it, you can’t tell what went wrong with your code.

This article has demonstrated how to enable PHP to debug with Xdebug and configure it in VSCode. You can also integrate it in Eclipse, PHPstorm, and other IDEs.

For those venturing into the realm of extensive coding, setting up a robust debugging environment is paramount. Especially when tackling intricate applications spanning thousands of lines, having a PHP debugger in place becomes essential to unearth errors and warning signs. With the assistance of advanced tools and the reliability of PHP web hosting, you’re equipped to navigate the complexities of coding and ensure a seamless development experience.

If there is anything you want to know more about, post your queries and suggestions in the comments section below.

How do I debug in PHP?

To debug in PHP, you can use tools like Xdebug, a powerful debugger and profiler for PHP. You can set breakpoints, inspect variables, and step through code. Alternatively, var_dump() and print_r() are commonly used functions to output variable values. For more complex debugging, you can use PHP’s error logging to track issues in the application.

How to enable PHP debug mode?

To enable PHP debug mode, set the display_errors directive to On in the php.ini file:

display_errors = On
error_reporting = E_ALL

This will display errors directly on the page. For production environments, it’s recommended to log errors to a file instead of displaying them on the screen, using log_errors = On and error_log = /path/to/logfile.log.

Is PHP easy to debug?

PHP is relatively easy to debug with the right tools. Simple debugging can be done with built-in functions like var_dump(), echo, or print_r(). However, for more complex issues, using advanced tools like Xdebug or PHP debugging in IDEs (such as PhpStorm or Visual Studio Code) can significantly improve the debugging process by offering breakpoints, stack traces, and variable inspection.

How to add debug to PHP?

A) To add debugging in PHP, use the following methods:

  • Error Reporting: Enable error reporting by adding these lines to your PHP script:
error_reporting(E_ALL);
ini_set('display_errors', 1);
  • Logging: Configure error logging in the php.ini or use error_log() to log errors to a file.
  • Xdebug: Install and configure Xdebug for more advanced debugging with breakpoints and profiling.
  • Debugging Functions: Use var_dump(), print_r(), or debug_backtrace() to inspect variables and trace execution flow.
Share your opinion in the comment section. COMMENT NOW

Share This Article

Shahzeb Ahmed

Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

Peak Performance.

Limitless Scalability.

  • 0

    Days

  • 0

    Hours

  • 0

    Mins

  • 0

    Sec

Off For 4 months
+40 free Migrations

Claim Now