jsuke06
jsuke06

Reputation: 145

How to change the version of php in PHP Intelephense?

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

Upvotes: 7

Views: 40046

Answers (5)

Az.Youness
Az.Youness

Reputation: 2632

I struggled with getting PHP 8.4 working properly in VS Code recently. After some testing, I found the solution that work for me!

Initially, I set up my .vscode/settings.json with:

{
  "intelephense.environment.phpVersion": "8.4",
  "php.validate.executablePath": "C:/php/v8.4.2/php.exe",
  "php.debug.executablePath": "C:/php/v8.4.2/php.exe"
}

But Intelephense still wasn't recognizing PHP 8.4 features. The fix turned out to be simple - I just needed to switch to the pre-release version of the PHP Intelephense extension:

 1. Open VS Code Extensions (Ctrl+Shift+X)
 2. Find PHP Intelephense
 3. Click the gear icon ⚙️
 4. Enable "Pre-release version"
 5. Restart VS Code

After this, everything worked perfectly! This seems to be a common fix - when new PHP features aren't recognized, try the pre-release version.

Upvotes: 0

Zollie
Zollie

Reputation: 1211

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

Upvotes: 2

Cris
Cris

Reputation: 457

Code -> Preferences -> Settings

Look for "php version". Then change it

Upvotes: 9

Charon ME
Charon ME

Reputation: 718

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.

Upvotes: 3

Herbert Peters
Herbert Peters

Reputation: 179

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.

Upvotes: 4

Related Questions