How to Change PHP Version Used by Composer
When you have multiple PHP versions installed on your system, such as in a WAMP environment where PHP 5.6 and PHP 7.0 coexist, relying on the global php.exe or the default path can lead to confusion. If your project requires PHP 7.0 but your current shell is invoking PHP 5.6, running composer install will fail with errors regarding missing extensions or unsupported syntax. The core issue isn't necessarily that Composer itself is hardcoded to a specific version, but rather how your system's environment variables dictate which interpreter runs when you type php.
To resolve this without reinstalling Composer, you must explicitly tell the shell which PHP binary to execute before invoking Composer. Composer determines its internal PHP version by running the php command found in your system's PATH. If that command points to PHP 5.6, Composer will use it regardless of what you might expect based on your WAMP control panel settings.
Using the Full Path to the Correct Interpreter
The most reliable method to switch versions immediately is to bypass the default php.exe and call the specific binary for the version you need. In a WAMP setup, these binaries are usually located in subdirectories like \wamp\bin\php\php5.6\php.exe or \wamp\bin\php\php7.0\php.exe.
You can construct a command that explicitly calls the PHP 7 binary:
"w:\wamp\bin\php\php7.0\php.exe" -v
Once you have confirmed that this specific binary returns PHP 7.0, you can invoke Composer using that same path. This ensures that Composer's internal check (php -v) reads the correct version:
"w:\wamp\bin\php\php7.0\php.exe" -v
composer --version
If you prefer to run Composer directly, ensure the command line tool (composer.phar or composer.exe) is configured to use the same PHP executable. You can do this by setting an environment variable or using a wrapper script. However, simply prefixing the command with the full path is often sufficient for ad-hoc tasks.
Modifying Environment Variables for Persistence
If you find yourself switching versions frequently, manually typing the full path every time is inefficient. A better approach involves modifying your system's environment variables to create an alias or a custom entry point.
On Windows, you can add a new variable named PHP_BIN that points to your desired interpreter:
set PHP_BIN="w:\wamp\bin\php\php7.0\php.exe"
Then, update your Composer configuration to use this variable whenever it runs. You can edit the global config file located at %APPDATA%\Composer\config.json (or create one if it doesn't exist) and add the bin directory or override the PHP path:
{
"config": {
"platform.check": false,
"vendor-dir": "vendor"
}
}
However, Composer does not natively read a PHP_BIN environment variable to switch its internal interpreter. Instead, you must ensure that the executable in your PATH is the correct one. You can adjust your system's Path environment variable to prioritize the PHP 7 directory over the PHP 5.6 directory, or create a batch file that temporarily modifies the PATH before running Composer:
@echo off
set PATH=w:\wamp\bin\php\php7.0;%PATH%
composer install
Understanding WAMP Control Panel Limitations
A common pitfall is assuming that changing the PHP version in the WAMP control panel automatically updates all open command prompts or IDE sessions. When you switch versions in WAMP, it typically updates the service running on port 80 and the default php.exe for the next terminal session. However, if your current terminal was opened before the switch, it retains the old environment variables and PATH settings. You will see PHP 5.6 persist until you restart the command prompt or clear your shell's environment cache.
Additionally, some IDEs like PhpStorm or Visual Studio Code may have their own php.path configurations in .json files that override system paths. Even if your shell is using PHP 7, your IDE might still be launching the CLI tool for Composer with a hardcoded path to PHP 5.6. Always verify which binary is actually executing by running which php or where php in your terminal and checking the output path against your WAMP installation directories.
Final Recommendation
If you need to isolate specific projects to different PHP versions, the cleanest solution is to use Composer's built-in ability to download a compatible version or to install Composer globally once and then create symbolic links or wrappers for each project directory that point to the correct PHP binary. For WAMP users, creating a simple batch script per project that sets the PATH and invokes Composer is often the most robust workaround without reinstalling the entire stack.
