Skip to content

Latest commit

 

History

History
 
 

PHP

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

PHP Debugging with PHP Debug and XDebug

by Luiz Barni (@odahcam)

This recipe shows how to use the PHP Debug extension with VS Code to debug PHP code with XDebug.

Getting Started

Fistly we you gotta install XDebug.

Installing XDebug

Let's to this by following the PHP Debug insctructions:

If you have pecl enabled, you can install XDebug via pecl install xdebug and jump to step 3.

  1. Install XDebug I highly recommend you make a simple test.php file, put a phpinfo(); 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.
  2. 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 your phpinfo() output under "Loaded Configuration File".

  3. 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 recommend remote_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.

  4. If you are doing web development, don't forget to restart your webserver to reload the settings.

  5. Verify your installation by checking your phpinfo() output for an XDebug section.

Installing PHP Debug

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.

Configure launch.json File

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:

add-php-debug

launch.json ready!

Create a simple PHP file

Now that we have everything configured let's just create a test file called index.php:

<?php

echo 'Hello new world!';

Start Debugging

  • 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.

php-xdebug-breakpoint