How to change the version of php in PHP Intelephense?
Asked Answered
F

4

6

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

Farthest answered 3/8, 2021 at 16:19 Comment(2)
"Edit in settings.json" should be itMontemontefiascone
Have you installed the php extension in your vs code ?Alejoa
A
3

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.

Averment answered 2/3, 2022 at 0:59 Comment(2)
This didn't work for me on Mac OS, but adding the below line to settings.json did: "php.validate.executablePath": "/usr/local/opt/[email protected]"Stephainestephan
@Stephainestephan That's because you don't use the PHP Intelephense extension, as the OP does.Boudicca
S
7

Code -> Preferences -> Settings

Look for "php version". Then change it

Sower answered 29/11, 2022 at 13:0 Comment(0)
A
3

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.

Averment answered 2/3, 2022 at 0:59 Comment(2)
This didn't work for me on Mac OS, but adding the below line to settings.json did: "php.validate.executablePath": "/usr/local/opt/[email protected]"Stephainestephan
@Stephainestephan That's because you don't use the PHP Intelephense extension, as the OP does.Boudicca
H
3

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.

Hydrofoil answered 24/3, 2022 at 12:11 Comment(1)
You should NOT have these directives available if you use PHP Intelephense. These belong to the builtin PHP extension which, according to Intelephense set up instructions, needs to be disabled.Boudicca
M
2

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

Mainland answered 19/1, 2023 at 16:56 Comment(9)
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.Seventieth
You SHOULD NEVER place any of the PHP folders on your Windows PATH if you are using WAMPServer!!!Mooneye
@Mooneye - 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!Mainland
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://mcmap.net/q/1329497/-problems-with-php-versions-on-xampp-and-vs-codeMooneye
And this may help as well #15597567Mooneye
@Mooneye - 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!Mainland
@Mooneye - and of course I’ll update my post with a short warning regarding this…Mainland
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.Mooneye
Please note that you should not have php.validate.executablePath directive available if you use PHP Intelephense, neither need to set the PHP interpreter path for Visual Studio Code to work. These belong to the builtin PHP extension which, according to Intelephense set up instructions, has to be disabled.Boudicca

© 2022 - 2024 — McMap. All rights reserved.