Skip to content
  1. LuzineDebug
  2. Guides
  3. Debug PHP with Xdebug 3 (PhpStorm, VS Code)
Guide Intermediate ≈ 20 min

Debug PHP with Xdebug 3 (PhpStorm, VS Code)

In short

Install Xdebug, set xdebug.mode=debug and xdebug.start_with_request=trigger, then make your IDE listen on port 9003. Start a session with the Xdebug Helper browser extension or the XDEBUG_SESSION parameter: PHP stops on your breakpoints.

Be the first to approve this guide

Why this method

dd() and var_dump() are fine for a simple bug, but get painful as soon as you need to follow a value through a middleware, a service and an SQL query. Xdebug turns PHP into a step-through program: breakpoints, variables, call stack, expression evaluation, for a web page as well as an Artisan command or a PHPUnit test. Configuration changed with Xdebug 3 (new port, new xdebug.mode directive), which explains most "it does not stop" issues.

What you need

  • PHP 8.1 or newer
  • PhpStorm, or VS Code with the PHP Debug extension (xdebug.php-debug)
  • Permission to edit php.ini or the Docker image

Step by step

  1. Install Xdebug

    On macOS with Homebrew PHP (and therefore Valet), or on Linux with a compiled PHP, use pecl install xdebug. On Debian and Ubuntu, the package is called php8.4-xdebug (adjust the version). With Laravel Herd, Xdebug is included in Herd Pro and is enabled from the Herd settings. On Windows, the official "Xdebug installation wizard" tells you which DLL to download from your php -i output. In Docker, add it to the image.

    Then check: php -v must print "with Xdebug v3.x".

    bash
    # macOS (Homebrew, Valet) / Linux
    pecl install xdebug
    
    # Debian / Ubuntu
    sudo apt install php8.4-xdebug
    
    # Dockerfile (image officielle php)
    RUN pecl install xdebug && docker-php-ext-enable xdebug
    
    php -v
    php --ini
  2. Configure Xdebug 3

    Find the right file with php --ini: the CLI and PHP-FPM often use different php.ini files. With start_with_request=trigger, Xdebug only starts when the request carries a trigger, so pages are not slowed down. The default port is now 9003 (9000 in Xdebug 2, often used by PHP-FPM).

    In Docker, client_host must point to the host machine: host.docker.internal on Docker Desktop; on Linux, add extra_hosts: ["host.docker.internal:host-gateway"] to the compose service. Call xdebug_info() in a page to check the active configuration.

    ini
    zend_extension=xdebug
    xdebug.mode=debug
    xdebug.start_with_request=trigger
    xdebug.client_host=127.0.0.1
    xdebug.client_port=9003
    ; Docker : xdebug.client_host=host.docker.internal
  3. Trigger a session from the browser

    Install the Xdebug Helper extension (Chrome, Firefox): in Debug mode it adds the XDEBUG_SESSION cookie to every request to the site. Without the extension, add ?XDEBUG_SESSION=1 to the URL. For an API called by an HTTP client, send the XDEBUG_SESSION=1 cookie. Xdebug then tries to connect to your IDE on port 9003; if nothing is listening, the page renders normally.

    bash
    curl --cookie "XDEBUG_SESSION=1" http://localhost:8000/api/orders
  4. Listen with PhpStorm

    In Settings > PHP > Debug, check that the Xdebug port is 9003. Click the Start Listening for PHP Debug Connections icon in the toolbar, set a breakpoint in a controller, then load the page with the trigger. On the first connection, PhpStorm offers to accept the incoming server.

    With Docker, open Settings > PHP > Servers and map the project folder to the path inside the container, for example /var/www/html, otherwise PhpStorm cannot tell which local file matches.

  5. Listen with VS Code

    Install the PHP Debug extension (publisher: Xdebug), then create .vscode/launch.json. Start the "Listen for Xdebug" configuration with F5, set a breakpoint and load the page with the trigger. pathMappings is only needed when PHP runs in a container or a VM: the key is the remote path, the value the local folder.

    json
    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Listen for Xdebug",
          "type": "php",
          "request": "launch",
          "port": 9003,
          "pathMappings": {
            "/var/www/html": "${workspaceFolder}"
          }
        }
      ]
    }
  6. Debug an Artisan command or a test

    On the command line there is no cookie: force the start with PHP’s -d option. The same syntax works on macOS, Linux and Windows. The IDE must be listening before you run the command. With PhpStorm and Docker, also set PHP_IDE_CONFIG="serverName=your-server-name" so it finds the path mappings.

    bash
    php -d xdebug.start_with_request=yes artisan app:import-orders
    
    php -d xdebug.start_with_request=yes vendor/bin/phpunit --filter=OrderTest
  7. Complement with dump(), dd() and Ray

    Xdebug is not always the fastest tool. In Laravel, dump($order) prints a value without stopping the request and dd($order) prints it and stops everything. Log::debug() writes to storage/logs/laravel.log, useful for queued jobs. Ray (a paid Spatie app) sends values to a separate window, handy for AJAX requests. Keep Xdebug for following a complex flow step by step.

    php
    dump($order->toArray());
    Log::debug('order total', ['id' => $order->id, 'total' => $order->total]);
    ray($order)->label('avant paiement');
  8. Profile a slow page

    To understand a slowdown, switch to xdebug.mode=profile: Xdebug writes a cachegrind.out.* file into xdebug.output_dir for every triggered request. Open it in PhpStorm (Tools > Analyze Xdebug Profiler Snapshot) or with QCacheGrind / KCachegrind to see the time spent in each function. The profiler slows PHP down a lot: enable it only while measuring, then go back to xdebug.mode=debug or off.

    ini
    xdebug.mode=profile
    xdebug.start_with_request=trigger
    xdebug.output_dir=/tmp/xdebug

    Xdebug must never be loaded in production: it slows PHP down and a debug session can expose code and data.

Mistakes to avoid

  1. Listening on port 9000: that was Xdebug 2. Xdebug 3 uses 9003, and 9000 is often taken by PHP-FPM.
  2. Editing the wrong php.ini: the CLI, PHP-FPM and Herd each have their own. Check with php --ini and xdebug_info().
  3. Keeping client_host=localhost in Docker: inside the container, localhost is the container itself, not your IDE.
  4. Forgetting path mappings: the connection is made, but no breakpoint is ever hit.
  5. Leaving start_with_request=yes on all the time: every request tries to reach the IDE and PHP slows down, even without debugging.

Checklist

Did this guide help you?

Approve it in one click: it will be highlighted for other developers.

Frequently asked questions

Why does Xdebug not stop on my breakpoints?

In order: Xdebug is not loaded, xdebug.mode does not include debug, the request has no trigger, the IDE is not listening on 9003, or paths do not match (Docker). xdebug_info() answers the first three.

Which port does Xdebug 3 use?

Port 9003 by default, configurable with xdebug.client_port. Xdebug 2 used 9000.

How do I debug an Artisan command with Xdebug?

Start listening in your IDE, then run php -d xdebug.start_with_request=yes artisan my:command. The breakpoint in the handle() method is hit.

Does Xdebug slow PHP down?

Yes, even when loaded without an active session. Set xdebug.mode=off or disable the extension when you are not debugging, and never install it in production.

Tools to apply this guide

More guides