Xdebug Cli



This tutorial will teach you how to debug files and applications to gain maximum efficiency and accuracy from your PHP code.

Start the debugging session as usual by clicking on the green bug button in the top right corner, set breakpoints in the code. When you start the command line script, PhpStorm connects to the Xdebug automatically, and you can debug your code. Xdebug's step debugger allows you to interactively walk through your code to debug control flow and examine data structures.

Zend Studio's Debugging feature can detect and diagnose errors in PHP code situated locally or on remote servers. The debugger allows you to control the execution of your program by setting breakpoints, suspending launched programs, stepping through your code, and examining the contents of variables.

Cli

Debugging should be used at stages where your scripts and applications are formed sufficiently to be tried and tested.

Tutorial Content

In this tutorial, you will learn:

  • To create a new local PHP project
  • To create a new PHP file
  • To set breakpoints
  • To debug a PHP file as CLI
  • To debug a PHP file as a Web application

Provided Items

  • Throughout the tutorial, you will be provided with code snippets to insert in your project.

Prerequisites

Xdebug client host docker
  • Zend Studio 12.0 or above which can be downloaded from the Zend Studio Downloads page. For information on installation, see the Zend Studio Installation Guide.
  • Zend Studio debugs using the Zend Debugger. If you would like to debug using the Xdebug extension, see Installing Xdebug and Configuring Zend Studio for Debugging with Xdebug

Xdebug Phpstorm Cli

Xdebug

Related Media

To better understand the procedures described in this tutorial, watch this video:

Our first step is to create a new local PHP project to work with in this tutorial.

To create a new local project:

  1. From the Menu-bar, select File | New | Local PHP Project.
    The New PHP Project dialog displays.
  1. Enter the name of your project, and click Finish.
    The new PHP 5.5 project is created, and appears in the PHP Explorer.
Step 2: Creating a New PHP File

Next, we will begin to develop our application by adding a new PHP file to the project.

To create a new PHP file:

  1. In the PHP Explorer view, select the newly created project.
  2. Right-click, and select New | PHP File.
    The PHP File creation dialog is displayed.
  1. Name the new file 'debug.php', and click Finish.
    The new file is added, and displayed in the PHP Editor and PHP Explorer views.
  2. Paste the following example code into the new file:
  1. Save the file.

By default, Zend Studio is configured to stop debugging at the first PHP line in the code. Our next step is to add an additional breakpoint to specify where in the code the debugging process should pause.

To add breakpoints in your code:

  1. In the new 'debug.php' file, locate line 37.
  2. Double-click the vertical ruler to the left of the line.
    -OR-
    Select the line, and go to Run | Toggle Breakpoint.
    -OR-
    Press Ctrl+Shift+B.
    A blue ball will appear, indicating that a breakpoint has been set.
  1. Save the file.

To learn how to add conditions to a breakpoint, see Setting Breakpoints.

We will now debug the file as a CLI application using Zend Studio's internal debugger.

To debug the file as a CLI application:

  1. Right-click the file in the PHP Explorer, and select Debug As | CLI Application.
  2. Click Yes if asked whether to open the PHP Debug Perspective.
    A number of views will open with information about your script, and the debugging process stops where your first <?php label appears.
  1. The Debug View (1) is where the debug stack trace is displayed and the debugging process can be monitored and controlled.
  1. In the toolbar, click the Resume icon to continue to the set breakpoint.
  2. In the toolbar, click the Step Into icon .
    The Debugger goes into the display_workers function, and advances to line 52.
  3. The Variables view (3) will now display various information about the relevant variables and parameters through which the function was reached.
  4. The Debug Output view will display the HTML output created up until the breakpoint, while the Browser Output view will show the current output to a browser.
  1. In the Debug view, click Resume until the debugging process is terminated.
    Notice that as the debugging process progresses, the Debug Output and Browser Output displays are updated.
  2. The console view will display any errors or warnings about your script. In this case, it will display a Notice about an undefined variable on line 53.
  1. In the top-right corner of the perspective, click on the PHP Perspective icon to return to normal editing mode.

To run the debugging process again with the same configuration, click the debug icon on the toolbar.

To change debugging configurations, in the menu-bar, go to Run | Debug Configurations.

Zend Studio also allows you to debug applications, projects or files that are located on a server. You can debug either the local (Workspace) copy of files or the server copy of files.

To debug as a Web application:

  1. Place your application on a Web server.
  1. Right-click the file in the PHP Explorer, and select Debug As | PHP Web Application.
    The Debug PHP Web Application is displayed.
  1. Make sure the URL is correct, and modify it if needed.
  2. Click OK.
  3. Click Yes if asked whether to open the PHP Debug Perspective.
    A number of views will open with information about your script, and the debugging process stops where your first <?php label appears.


  1. Select the Browser view (tabbed with the Editor view).
    This view shows the output of the script to the browser. This will be updated as the debugging process continues. For now though, it will be empty.
  2. In the toolbar, click the Resume icon to continue the debugging.
    The debugger stops at the set breakpoint on line 37.
  3. Click the Resume icon again.
    The debugging is terminated, and the Browser view displays the output of the file.

To run the debugging process again with the same configuration, click the debug icon on the toolbar.

To change debugging configurations, in the menu-bar, go to Run | Debug Configurations.

Copyright © 2017Rogue Wave Software

Debugging: Configure VS Code + XDebug + PHPUnit

I've lost track of how many hours I have spent trying to configure and use Xdebug. The process usually begins with me noticing how often I've been using dd() while re-running my PHPUnit tests. I think to myself, 'There has to be a better way.' The next thing I know, I'm researching Xdebug blog posts, reading the documentation, and getting frustrated that I still can't get it to work. Hours later, I end up closing my open browser tabs and going back to littering my codebase with dd(). Except now, with a bruised ego.

Don't get me wrong, dd() is a wonderful tool that I will continue to use. However, there are instances where a real debugging setup can improve my efficiency tenfold. Using Xdebug, I can step through code while watching values change without having to add a single dd() entry and without the need to run the tests multiple times. The cost and time savings add up quickly! There have also been occasions where, in the process of stepping through client code, I find myself in Laravel's core code. I am usually enlightened with the inner workings of Laravel, which is an added benefit.

Why don't more developers use Xdebug in their development workflow?

From my experience, many developers don't fully understand the Xdebug architecture and its use cases. Xdebug is incredibly powerful and has many underutilized features. (You can read about all of the Xdebug features here.) In this post, though, I'm focusing on one specific feature called Remote Debugging (a misleading name since we can debug just fine without a remote system).

You can trigger Remote Debugging in two ways:

  1. Via the Browser
  2. Via the Command Line

Almost all of the resources I have come across for using Xdebug cover the first use case, via the browser. However, I want to use it in the terminal by running PHPUnit tests. After many hours of stumbling around, I found the configuration that works to use Xdebug Remote Debugging with one of the more popular editors out today, Visual Studio Code.

If you're using Laravel Homestead to develop locally, you may want to read this post to get started.

Getting Configured: Let's Do This!

We will need to configure the Xdebug PHP Extension, Visual Studio Code, and your Terminal for this to work. These steps assume that you are running tests on the same machine that you are editing with.

Xdebug

1. Install the Xdebug PHP Extension.

For MacOS users, you can easily install the Xdebug extension for your version of PHP using Homebrew. If you're using PHP 7.1, you can run:

For other platforms, you can read about how to install Xdebug here.

2. Find the path to your ext-xdebug.ini file by running:

The output should look like this:

3. Open ext-xdebug.ini to make sure the extension is loaded, and enabled.

It should look like this:

For CLI Remote Debugging, these are all the configuration settings we need in the ext-xdebug.ini file.

Visual Studio Code

1. Install the PHP Debug plugin.

2. Open the debug panel.

3. Click on the 'config' button (the cogwheel) and select PHP.

There is no need to modify the default launch.json file.

4. Open a PHPUnit test file and set a breakpoint using the debugger plugin.

5. Set the debugger to Listen for Xdebug.

6. Start Debugging by clicking on the green arrow button.

This doesn't actually start the debugging process, but it starts a listener that will be triggered once you run the PHPUnit test.

You should now see a step toolbar with the blue arrow buttons greyed out:

Terminal

Create an environment variable for XDEBUG_CONFIG and set the value to 'idekey=VSCODE'.

This variable flag will inform the Xdebug listener that the script is running. You can do this in one of two ways:

Export Command

Running this export command will create an environment variable for the current terminal session. It will revert once the session is complete.

- or - Modify your .bashrc/.zshrc file. (Preferred)

This is a more permanent solution for setting terminal environment variables:

  • open ~/.bashrc or ~/.zshrc.
  • insert the line export XDEBUG_CONFIG='idekey=VSCODE'.
  • save and close the file.
  • run source ~/.bashrc or source ~/.zshrc to pick up your changes.
Xdebug clipXdebug cli docker

Debug!

You're now ready to run your test:

Xdebug Client

Visual Studio Code should display the first breakpoint and the toolbar will allow you to step through your code.

This is just scratching the surface.

Xdebug is a powerful tool that can be intimidating to use at first—hopefully, this post will help reduce the barrier to entry for proper debugging. By doing so, you can harness a small sliver of Xdebug's power, become a more efficient developer, and gain the confidence to delve deeper into Xdebug if you choose to do so.

A quick note: If you are using the VSCode-PHPUnit plugin to run your tests, the debugger may be set to hit more breakpoints than you expected. To resolve this, you can uncheck the 'Everything' option in the debug panel:

Xdebug Cli

Happy Debugging!!

Xdebug Client

We'd love to hear your feedback! Tweet @JoseCanHelp and @TightenCo.