Laravel新建项目提示PHP版本不符,CLI却显示PHP7.2.0的问题
Hey there, let's break down this confusing issue you're facing: you've got PHP 7.2.0 installed via XAMPP, but when trying to create a Laravel project, the system keeps thinking you're running PHP 5.6.24. Here's what's going on and how to fix it:
The Root Cause
When you run php -v inside C:\xampp\php, you're directly calling that specific PHP executable. But when you run laravel new from C:\xampp\htdocs, your system is using the globally configured PHP version (which is 5.6.24) instead of XAMPP's 7.2.0. This happens because Windows prioritizes the first PHP executable it finds in your system's Path environment variable.
Step-by-Step Fix
1. Confirm the Global PHP Version
First, verify this is the issue by opening a brand new Command Prompt window (don't navigate to C:\xampp\php), then run:
php -v
You'll likely see it output PHP 5.6.24—this confirms the global version mismatch.
2. Update Your System's Environment Variables
To make Windows use XAMPP's PHP by default:
- Open Control Panel → System → Advanced System Settings → Environment Variables
- In the System Variables section, find and edit the
Pathvariable - Locate any entries pointing to other PHP installations (like the one for 5.6.24), then move the
C:\xampp\phpentry above them - Save your changes and close all open Command Prompt windows (this is critical—environment changes don't take effect in already open terminals)
3. Verify the Fix
Open a new Command Prompt window, run php -v again from any directory. It should now show PHP 7.2.0. Now head back to C:\xampp\htdocs and run:
laravel new site
This should complete without those version mismatch errors.
Alternative Temporary Fix
If you don't want to adjust environment variables right now, you can explicitly use XAMPP's PHP to run the Laravel installation command:
C:\xampp\php\php.exe C:\path\to\composer.phar create-project laravel/laravel site
Just replace C:\path\to\composer.phar with the actual path to your Composer executable on your system.
内容的提问来源于stack exchange,提问作者Boutamente abdessamad

