How to add the path to PHP in Visual Studio Code

I’ve recently started using Microsoft’s Visual Studio Code on my Mac. It works great, and I really like its simple useful features. The installation and most of the configuration worked flawlessly on my Mac and I was up and running very quickly. I had assumed it would be the same on Windows, but of course that wasn’t the case. Nothing is ever easy in Windows, is it?

The error I got was with two PHP extensions: PHP Executable not found. Install PHP 7 and add it to your PATH or set the php.executablePath setting.

Hey sure, I get that. The extension needs the location of my local PHP installation. No problem, I can provide that… but how??? You click on Open Settings and expect to just navigate there – but that would be too easy. All we get is this:

Then you think, ah OK, it must be in that mysterious settings.json file, but all that does is to open a new coding window, with two curly braces. How the hell is anyone going to know what the syntax is? It surprises me that the rest of Visual Studio Code is so good, and something as simple as am option to browse to (or even find itself) what some component is looking for isn’t there? Oh Microsoft, you haven’t changed a bit in 30 years!

Well here’s what to put in that settings.json file:

{
    "php.validate.executablePath": 
    "C:\\xampp\\php\\php.exe"
}

I know. Crazy. Not a syntax I would have dreamt up in 100 years. This can be written on the same line, and it looks like it’s a key value pair to me. The second parameter points to my local XAMPP installation, so feel free to point it to your own PHP location.

Bizzarely, instead of regular backslashes, make sure to use double-backslashes. Because… you know, reasons. Escaping things or whatnot, I have no idea. All I can tell you is that it works.

This tip comes courtesy of Mohamed Elrashid on StackExchange. Thank you, Mohamed!

Note to self: I believe had PHP been set as an environment variable to PHP been set in System Properties, there is a chance that Visual Studio Code would have found it upon first run. That’s what happened on my Mac, so I’m assuming it’s possible on Windows as well. It’s also what that mysterious error message at the beginning alludes to.

You can leave a comment on my original post.