It turns out you can run PHP code directly inside VS Code if you hook it up to your local PHP interpreter. No Debug Module Needed! It’s quick and simple to do and is an invaluable tool to have when you want to tinker with your code. Instead of booting up a web server and browser, you can execute instantly without leaving the VS Code IDE.

Prerequisites

To follow this tutorial step-by-step, make sure you have the following in place.

Visual Studio Code installed.

A version of PHP placed somewhere on your computer. Preferably PHP 7.0+.

A folder and a blank index.php file.

Configuring VS Code To Run PHP Code

So, we’ve got everything together and VSCode open, let’s add the settings.

1. Go to File -> Preferences -> Settings.

2. Add the following JSON to the User Settings on the Right Hand Side of the IDE. Replacing C:\\xampp\\php\\ with your path to your php.exe.

{ "launch": { "configurations": [ { "type": "php", "request": "launch", "name": "Run using local PHP Interpreter", "program": "${file}", "runtimeExecutable": "C:\\xampp\\php\\php.exe" }], } }

It should now look similar to below –

3. Save it.

4. Go to your index file and add the following code –

<?php $msg = "Hi Debug Console"; echo $msg; ?>

5. Press F5 and VS Code will launch and execute the script. However, if you’ve already got debugging launch settings installed, you will have to explicitly choose ‘Run using local PHP Interpreter‘ from the drop-down list that pops up when pressing F5.

6. The debug console will pop up at the bottom of the IDE and say, ‘Hi Debug Console’. See the image below.

7. That is it! You have successfully set up your local PHP interpreter to execute code directly in VS Code IDE!

Summary

This setup is really great if you want a quick mess around with your code. Without having to configure a web server or any debug modules. It’s a great little gem I’ve stumbled across whilst using this fantastic IDE. I hope it helps you too!

The Author: Dan Englishby

Post navigation