如何在PhpStorm中真正切换PHP版本(非常规设置路径)
Hey there, let’s work through why you’re still hitting that PHP 5.6 compatibility error even after updating PhpStorm’s settings. I’ve been in this exact spot before, so let’s break it down step by step:
First off, a key thing to remember: PhpStorm’s PHP version setting only affects code highlighting and syntax checks in the IDE — it doesn’t change the actual PHP runtime that Composer uses when you run commands. That’s why your PHPUnit install is still complaining about PHP 5.6.
1. Start with your composer.json file (this is critical)
Composer relies on the version constraints defined in your composer.json to validate dependencies. Open up that file and look for the require section — you’ll probably see a line like this:
"require": { "php": ">=5.6", // ... other Symfony dependencies }
This tells Composer your project is supposed to work with PHP 5.6 or higher, but when installing PHPUnit 7.1 (which requires PHP 7.0+), Composer might still be leaning on the original constraint or detecting the wrong PHP runtime. Update that line to match your target version:
"php": ">=7.1",
2. Check which PHP version Composer is actually using
Chances are, your terminal is still pointing to PHP 5.6, even if PhpStorm is set to 7.1. To confirm, run this in your project’s root directory:
php -v
If it shows PHP 5.6, that’s the problem. You need to switch your terminal’s PHP runtime to 7.1:
- Windows: Adjust your system PATH to put the PHP 7.1 folder before the PHP 5.6 one, or use your local server tool (XAMPP/WAMP) to switch active versions.
- macOS/Linux: Use
update-alternatives(Linux) orbrew link php@7.1(Homebrew on macOS) to set 7.1 as the default. For per-project version management, tools likephpbreworphpenvwork great too.
You can also verify Composer’s PHP version directly with:
composer -V
This will show you the PHP version Composer is using under the hood.
3. Sync PhpStorm with your correct PHP executable
Even though PhpStorm’s setting doesn’t control the runtime, you should make sure it’s pointing to the same PHP 7.1 executable you’re using in the terminal:
- Go to
Settings > Languages & Frameworks > PHP - Under "CLI Interpreter", click the
...button and select/add the path to your PHP 7.1 executable (e.g.,C:\php71\php.exeor/usr/bin/php7.1) - Set the "PHP language level" to 7.1 to get proper syntax hints and avoid IDE errors.
- If you use PhpStorm’s built-in terminal, restart it after making changes — it should now use the correct PHP version.
4. Update dependencies and install PHPUnit
Once everything is aligned, run these commands to refresh your dependencies and install PHPUnit 7.1:
composer update composer require --dev phpunit/phpunit:7.1.*
This should resolve the compatibility error, since Composer will now recognize your PHP 7.1 runtime and the updated project constraints.
内容的提问来源于stack exchange,提问作者Jacke Dow

