5

I'm using iis and vscode to do php work. I recently updated iis from php7.3 to php7.4. My problem is that vscode is still using php7.3. I googled how to change the version in vscode and almost all the resources say that there's a JSON file that can changed under php settings in File > preferences > settings. When I search for php I'm not seeing any of these options. What am I doing wrong?

enter image description here

enter image description here

jsuke06
  • 125
  • 1
  • 2
  • 8

4 Answers4

4

Code -> Preferences -> Settings

Look for "php version". Then change it

Cris
  • 347
  • 3
  • 7
2

You can set the value directly as seen in your screenshot. Entry is named PHP Version. If you do so for that particular workspace, a file will be created at

{path_to_workspace}/.vscode/settings.json

If you don't see the folder .vscode in Explorer, you have to change the settings in Explorer to see hidden folders.

If you did set the PHP version on the page in your screenshot for that particular workspace then you should find in settings.json an entry that looks like the following

{
  "intelephense.environment.phpVersion": "8.1.0"
}

change that to

{
  "intelephense.environment.phpVersion": "7.4.0"
}

If you want to change the setting globally for all of your projects you can do the the following:

Open Explorer, or command line (cmd.exe) and type or copy the following line

%APPDATA%\Code\User\settings.json

Usually this will start vscode if not already running and open settings.json.

In that file at the beginning add after the opening bracket

  "intelephense.environment.phpVersion": "7.4.0",

  ... rest of the existing settings.json file here

Before you change these files manually, it might be a good idea to make a backup of these files.

  • This didn't work for me on Mac OS, but adding the below line to settings.json did: `"php.validate.executablePath": "/usr/local/opt/php@8.0"` – smohadjer Oct 24 '22 at 13:20
1

While you can set the version for the intelephense addon, this won't help much because it appears that this setting doesn't apply to VSC itself. However you can set the php.validate.executablePath setting to the path to your php.exe file, for example "php.validate.executablePath": "C:/wamp64/bin/php/php7.4.9/php.exe". This should override a possibly wrong php path VSC takes from the system PATH env setting when php.validate.executablePath is not set.

Charon ME
  • 698
  • 10
  • 22
0

Since the above answers did not solve my problem, I'd like to give an additional solution that handled my issue of having the right PHP version in VS code. I got to this problem during installation of Laravel and the terminal in VS code was giving me the error that my platform is using PHP 7.4 and Laravel 9 requires PHP 8.0.2. It just caused me a headache to figure out where VS Code is taking the info that my platform is using PHP 7.4 (however my WAMP server was using PHP 8.2) (and I also set in VS code settings, according to the above answers, the following:

  1. "php.validate.executablePath": "C:/wamp64/bin/php/php8.2.0/php.exe"
  2. "intelephense.environment.phpVersion": "8.2.0",

But during creating a new Laravel project, VS code terminal still gave me the above error and did not recognize my PHP 8.2 version.

So my final working solution in Windows 11 (same in windows 10) was to setting the Path variable (to PHP executable) in Environment Variables.

1. in windows Start menu click and search for Environment Variables -> you can just open that control panel and click and open the Environment Variables settings:

enter image description here

2. You will get a new modal window and you have to click on Path in System variables like in the image below:

enter image description here

3. and you will find the PHP path and you have to just edit the path value to your own required PHP folder and then click OK. (I am not sure whether it is optional but I restarted my VS Code editor and everything was OK with PHP version from then on in VS Code regarding PHP version).

enter image description here

Some important warning on this

I’ve been using WAMP for a bit more than 10 years ago now as far as I remember, never had any problem with setting PHP path variable in windows, and only in the last few years WAMP raises a warning when you set a PATH variable to PHP executable in windows, since WAMP does not need that and also some developers considers that a bad practice, most importantly because it is a bit unnecessary and later on, it could cause some problems as well if we forget that setting in windows and we want to use another version of PHP. So the usual problems, it is not because of an ugly security reason in windows first of all, but because we as developers have very bad memories. It is just increasing our stupidity factor that can influence our life in the future. And there are more optimal solutions on this issue.

My suggestion is to check this subject a little more deeply as someone has a short time and you can read more details about how you can set PHP versions for VS Code. So my solution above is good, no problem with that as a short term solution, however keep this warning in mind too when you solve this issue and study this subject a little more here on SO.

Helpful links I have read on this too:

How To Run PHP From Windows Command Line in WAMPServer

And this one if you want to launch VS Code with different PHP versions, very helpful post:

Problems with php versions on xampp and vs code

And this one:

How To Run PHP From Windows Command Line in WAMPServer

Zollie
  • 1,171
  • 7
  • 14
  • You also need to restart vscode too. I had the problem where command prompt changed php version, but vscode terminal didn't. But after closing and reopening vscode it worked. – Studocwho Apr 15 '23 at 13:08
  • You SHOULD NEVER place any of the PHP folders on your Windows PATH if you are using WAMPServer!!! – RiggsFolly Apr 16 '23 at 10:32
  • @RiggsFolly - would you be so kind to provide me a link with more detailed explanation on this, please. i’ve been using WAMP only for local development purposes in the last 10 plus years, never had any problem with that. Wamp gives an alert on this if/when there is a path set in windows to PHP - in the last couple of years but I have not seen a longer explanation why it is that bad and where the PHP folder should be on windows if wamp is only for local purposes. Thank you if you take your time for a link on this! – Zollie Apr 16 '23 at 11:21
  • Basically, because you can have 10+ versions of PHP installed within one instance of WAMPServer, putting only one on the PATH is a) unnecessary b) something that will be forgotten when you want to use a different version of PHP. Does this help you to a solution? https://stackoverflow.com/a/76027201/2310830 – RiggsFolly Apr 16 '23 at 11:30
  • And this may help as well https://stackoverflow.com/questions/15597067/how-to-run-php-from-windows-command-line-in-wampserver/16289254#16289254 – RiggsFolly Apr 16 '23 at 11:31
  • @RiggsFolly - thank you for your time on this and your fast reply! i just started to read your answers on SO regarding this so I will study this a bit more. However, I am locally using more versions of PHP (with wamp, fcgi) on my windows during development tasks on different sites, and I did not experienced any problem yet on this, but I will check this a bit more and get back to you as I have any more relevant questions on this. Thanks again for your time! – Zollie Apr 16 '23 at 11:36
  • @RiggsFolly - and of course I’ll update my post with a short warning regarding this… – Zollie Apr 16 '23 at 11:41
  • 1
    I have to say this is mostly aimed at beginners. If you know what you are doing it does no harm to have this in the path. I will pretend I didnt say this in an open comment. – RiggsFolly Apr 16 '23 at 11:43