Switch branches/tags
Find file History
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
README.md add some extra information to debug with PHP Oct 31, 2018

README.md

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

Firstly, you need to install XDebug.

Installing XDebug

Let's do this by following the PHP Debug instructions:

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 appropriate 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 web server 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 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

Extra: Incrementing launch.json (running and debuging with vscode)

You can specify some properties in launch.json configuration to make it easier to run and debug PHP code with one click.

This is the default config generated by PHP Debug extension

{
    "name": "Listen for XDebug",
    "type": "php",
    "request": "launch",
    "port": 9000
},

You can increment it like below to run and debug at the same time, just hitting F5 on vscode.

{
    "name": "Launch server",
    "type": "php",
    "request": "launch",
    "program": "${workspaceRoot}/path/to/server.php",
    "runtimeExecutable": "php",
    "runtimeArgs": [
        "-dzend_extension=path/to/xdebug",
        "-S",
        "0.0.0.0:8000"
    ],
    "cwd": "${workspaceRoot}",
    "port": 9000
}

This will start the initial file informed in program, use xdebug passed in runtimeArgs to debug the application and serve it on all interfaces 0.0.0.0 (you can change it to localhost).