Wednesday, May 27, 2015

Run PHP from Visual Studio Code

In my previous posts I explained examples using a solution file to compile and debug .NET (mono) in Visual Studio Code.

This post shows how to set up a task to run PHP using the build in Development Server of PHP.

In Visual Studio Code open the folder with the php file(s).

Then start the Command Palette and choose Configure Task Runner.
This creates a tasks.son file. Clear all and replace with the settings below:

{
"version": "0.1.0",
"command": "php",
"args": [
"-S", "127.0.0.1:8080",
"-t", "${workspaceRoot}"
]
}

Save the file and start the task from the Command Palette, choose Run Task and then 'php'.

A PHP Development Server has been started on port 8080. Start your browser, go to http://127.0.0.1:8080 and you will see the website from the selected folder.



At this moment it is not possible to debug PHP from Visual Studio Code.

7 comments:

Unknown said...

I downloaded VS code, did the above but got this error "Failed to launch external program php -S 127.0.0.1:8080 -t ${workspaceRoot}. spawn php ENOENT"

do you have any advice for this?

Frank said...

Do you have php installed? Is it in your path? Try to run php from the terminal.

James Blanc said...

I'm a writer and I frequently use PC and additional programs to improve my skills. I have series of articles on a topic of writing tips and Educational features and even instructions on write a creative brief.

Kevin Long said...

this might be stupid question, but how do you stop the running task.

Álvaro G. Vicario said...

There isn't any "Configure Task Runner" command in my copy of Visual Studio Code so I guess it's an optional extension and you explain how to install it in some other post. It'd be cool if you could edit current post and add a link.

Frank said...

Álvaro, I wrote this post a year ago based on the first release of VS Code (v0.1) which did not have extensions at the time. A lot has changed in a year and one of the major changes were the extensions. At the moment there are several extensions available for PHP. Please refer to https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug which is an extension for debugging PHP sites.

Álvaro G. Vicario said...

Thanks for the heads up, Frank. FYI, I eventually found an extension in the Marketplace ("Runner") that allows running current file as command-line script, which is was I was looking for in the first place.