CMD picking the wrong PHP version

CMD picking the wrong PHP version

Sometimes CMD picks the wrong version of PHP and not the one we install via XAMPP or MAMP.

Let’s say you have downloaded and installed XAMPP or MAMP with the latest PHP. But when you run the command “php -v” in your terminal, it displays the wrong version of PHP (typically the previously installed version of PHP). This is a common issue when you try to upgrade your PHP version. Previously, you could have upgraded the PHP version easily using Homebrew. But nowadays, Homebrew itself has become too complex and it has its errors.

Problem with Laravel 10

You will also face that error when you try to create a project in Laravel 10+ version. Because Laravel 10 requires PHP greater than 8 and a lot of developers are using PHP 7. The reason why developers are still using PHP 7 is because most of the shared hosting servers are still running on PHP 7. As PHP 8 is a big upgrade, a lot of things have been changed. So changing the PHP version could crash the entire project. On the local computer, it’s fine. But if you have a live website or an API, a crashed website means losing a lot of users and potentially a lot of customers which results in a loss of revenue (which no one wants).

Updating PHP version

To update the PHP version, you simply need to open CMD anywhere in your computer and run the following command:

export PATH="/Applications/MAMP/bin/php/php8.2.0/bin:$PATH"

Here, you can change the path to your own PHP file. Typically, it is in your XAMPP/MAMP “bin” folder. Inside “bin”, you will find the “php” folder. Inside that folder, you can go to your desired PHP version folder. And every PHP version folder also has a “bin” folder in it. Go it in and paste its path there.

The above line is for Mac users. For Windows users, the path will look like:

export PATH="C:\XAMPP\bin\php\php8.2.0\bin:$PATH"

Check PHP version

After that, you need to close the terminal and open it again. Then run the following command to verify the PHP version:

php -v

Now, you should be able to see your selected PHP version.

So that’s it. That’s how you can upgrade your PHP if your CMD is picking the wrong PHP version. If you face any issues with this, kindly do let me know.