Using Visual Studio Code For PHP Development

For a consulting engagement, I needed to be able to run a debugger against a PHP codebase as part of a migration project. Because my last experience with PHP was sometime last decade, I had no clue how to do this. Now I have a slight clue, so I figured I’d share that slight clue. Note that this is for installing PHP on Windows; the Linux experience is a bit more refined.

Step One: Install xampp

Grab the latest version of xampp at https://www.apachefriends.org/download.html.  As of the time of writing, the latest PHP version is 7.3, but grab the version for PHP 7.2 because the debugger DLL we will use does not currently support PHP 7.3.

Run the xampp installer and install in C:\xampp or a similar directory.  The only services we need are Apache httpd and PHP; you do not need to install MySQL, Tomcat, or any of the other services or administration tools.

Download xdebug

Xdebug is a debugger DLL for PHP, which you can download from http://xdebug.org.  We want to get the latest 32-bit, thread-safe, stable version which supports PHP 7.2 and is built using the Visual C++ 2017 redistributable.  As of 2018-12-28, that is http://xdebug.org/files/php_xdebug-2.6.1-7.2-vc15.dll.

The DLL we want.

After you have downloaded the appropriate DLL, copy it into C:\xampp\php\ext\ (assuming you installed to C:\xampp).  Then, open C:\xampp\php\php.ini and add the following lines to the bottom of the file:

[XDebug]
zend_extension = "C:\xampp\php\ext\php_xdebug-2.7.0beta1-7.3-vc15-nts-x86_64.dll"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1

Configure Visual Studio Code

Visual Studio Code has built-in PHP support, but there are a couple of plugins which I like, specifically, PHP Intelephense and PHP Debug.

The two extensions I used for PHP debugging.

You can install these extensions by opening up Visual Studio, clicking the Extensions option (highlighted in the image above), and typing the name of the extension into the search box.  Click on the “Install” button next to each extension and when all extensions are installed, close and re-open Visual Studio Code.

Then, press the F1 key and type “User” into the console. This will give you a few options:

Computer: read my mind.

From that list, you want to select “Preferences: Open User Settings.” That will pop up the following window:

A whole bundle of things you can change to break your Visual Studio Code installation.

There are a number of settings you might want to configure here, but for now, type “PHP” into the search settings box above.  We need to validate the PHP executable path, so click the “Edit in settings.json” link:

PHP-specific user settings.

Clicking on the settings.json link will bring you to a side-by-side editor.  The left-hand side is immutable, but you can override its values on the right-hand side.  Set php.validate.executablePath to your installed PHP executable, making sure to double up any backslashes. In my case, it is:

"php.validate.executablePath": "C:\\xampp\\php\\php.exe"
Setting an external path.

Configure httpd

Navigate to C:\xampp\apache\conf (assuming you installed Xampp in C:\xampp) and open the httpd.conf file in a text editor.  In the “DocumentRoot” section, you will want to replace whatever is there with the root directory for your PHP code.

After you do that, open up the Xampp control panel and start the Apache service:

The Xampp control panel running Apache + PHP.

Configure VS Code For Debugging

In Visual Studio code, go to the Debug tab and ensure that you have two Debug configuration options: “Listen for XDebug” and “Launch currently open script.”

Two debugging configurations should be available.

If you do not have these two, click on the “Add Config (workspace)” and add the following JSON:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

Then, select Listen for XDebug and click the Play button.  This will create a hook between Visual Studio Code and your debugger over port 9000.  You might need to create a firewall rule to allow inbound traffic on port 9000.

From here, you can put add and remove breakpoints and by navigating to http://127.0.0.1/{mypage.php}, you can trigger the debugger because we have the XDebug DLL installed.

The Visual Studio Code documentation for debugging includes detail on how to navigate the debugger.

Advertisements

One thought on “Using Visual Studio Code For PHP Development

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s