This recipe shows how to use the PHP Debug extension with VS Code to debug PHP code with XDebug.
Fistly we you gotta install XDebug.
Let's to this by following the PHP Debug insctructions:
If you have
pecl
enabled, you can install XDebug viapecl install xdebug
and jump to step 3.
-
Install XDebug I highly recommend you make a simple
test.php
file, put aphpinfo();
statement in there, then copy the output and paste it into the XDebug installation wizard. It will analyze it and give you tailored installation instructions for your environment. In short:- On Windows: Download the appropiate precompiled DLL for your PHP version, architecture (64/32 Bit), thread safety (TS/NTS) and Visual Studio compiler version and place it in your PHP extension folder.
- On Linux: Either download the source code as a tarball or clone it with git, then compile it.
-
Configure PHP to use XDebug by adding
zend_extension=path/to/xdebug
to your php.ini. The path of your php.ini is shown in yourphpinfo()
output under "Loaded Configuration File". -
Enable remote debugging in your php.ini:
[XDebug] xdebug.remote_enable = 1 xdebug.remote_autostart = 1
There are other ways to tell XDebug to connect to a remote debugger than
remote_autostart
, like cookies, query parameters or browser extensions. I recommendremote_autostart
because it "just works". There are also a variety of other options, like the port (by default 9000), please see the XDebug documentation on remote debugging for more information. -
If you are doing web development, don't forget to restart your webserver to reload the settings.
-
Verify your installation by checking your
phpinfo()
output for an XDebug section.
We got some options for doing this here:
-
Via CLI:
code --install-extension felixfbecker.php-debug
. -
Via VSCode's extension menu (Ctrl / ⌘ + Shift + X): search for
felixfbecker.php-debug
. -
Via VSCode extension store: go to PHP Debug in VSCode store and click the "Install" button.
Click on the Debugging icon in the Activity Bar to bring up the Debug view. Then click on the gear icon to configure a launch.json
file, selecting PHP for the environment:
launch.json
ready!
Now that we have everything configured let's just create a test file called index.php
:
<?php
echo 'Hello new world!';
-
Set a breakpoint in index.php on the line 3 (
echo
's line). -
Launch a PHP server for the file.
php -S localhost:4000 index.php
The
localhost
's port (4000
) can be anything you want. -
Go to the Debug view, select the "Listen for XDebug" configuration, then press F5 or click the green play button.
-
Open the browser in http://localhost:4000.