Debug PHP with Xdebug 3 (PhpStorm, VS Code)
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.
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
-
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 calledphp8.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 yourphp -ioutput. In Docker, add it to the image.Then check:
php -vmust print "with Xdebug v3.x".# 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 -
Configure Xdebug 3
Find the right file with
php --ini: the CLI and PHP-FPM often use different php.ini files. Withstart_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_hostmust point to the host machine:host.docker.internalon Docker Desktop; on Linux, addextra_hosts: ["host.docker.internal:host-gateway"]to the compose service. Callxdebug_info()in a page to check the active configuration.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 -
Trigger a session from the browser
Install the Xdebug Helper extension (Chrome, Firefox): in Debug mode it adds the
XDEBUG_SESSIONcookie to every request to the site. Without the extension, add?XDEBUG_SESSION=1to the URL. For an API called by an HTTP client, send theXDEBUG_SESSION=1cookie. Xdebug then tries to connect to your IDE on port 9003; if nothing is listening, the page renders normally.curl --cookie "XDEBUG_SESSION=1" http://localhost:8000/api/orders -
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. -
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.pathMappingsis only needed when PHP runs in a container or a VM: the key is the remote path, the value the local folder.{ "version": "0.2.0", "configurations": [ { "name": "Listen for Xdebug", "type": "php", "request": "launch", "port": 9003, "pathMappings": { "/var/www/html": "${workspaceFolder}" } } ] } -
Debug an Artisan command or a test
On the command line there is no cookie: force the start with PHP’s
-doption. The same syntax works on macOS, Linux and Windows. The IDE must be listening before you run the command. With PhpStorm and Docker, also setPHP_IDE_CONFIG="serverName=your-server-name"so it finds the path mappings.php -d xdebug.start_with_request=yes artisan app:import-orders php -d xdebug.start_with_request=yes vendor/bin/phpunit --filter=OrderTest -
Complement with dump(), dd() and Ray
Xdebug is not always the fastest tool. In Laravel,
dump($order)prints a value without stopping the request anddd($order)prints it and stops everything.Log::debug()writes tostorage/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.dump($order->toArray()); Log::debug('order total', ['id' => $order->id, 'total' => $order->total]); ray($order)->label('avant paiement'); -
Profile a slow page
To understand a slowdown, switch to
xdebug.mode=profile: Xdebug writes acachegrind.out.*file intoxdebug.output_dirfor 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 toxdebug.mode=debugoroff.xdebug.mode=profile xdebug.start_with_request=trigger xdebug.output_dir=/tmp/xdebugXdebug must never be loaded in production: it slows PHP down and a debug session can expose code and data.
Mistakes to avoid
- Listening on port 9000: that was Xdebug 2. Xdebug 3 uses 9003, and 9000 is often taken by PHP-FPM.
- Editing the wrong php.ini: the CLI, PHP-FPM and Herd each have their own. Check with
php --iniandxdebug_info(). - Keeping
client_host=localhostin Docker: inside the container, localhost is the container itself, not your IDE. - Forgetting path mappings: the connection is made, but no breakpoint is ever hit.
- Leaving
start_with_request=yeson 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
- How to read a stack trace (JS, Python, PHP, Java)
- Debug JavaScript with Chrome DevTools
- Debug Node.js with breakpoints (VS Code, Chrome)
- Read Docker container logs and debug a crashing container
- Debug an Arduino with the Serial Monitor
- Find the commit that broke your code with git bisect
- How to create a minimal reproducible example