We can download and install PHP using the executable file on Windows from its official website, however, no need to do that when we can use the command prompt or Powershell.
PHP is not some new or unknown scripting language, it has been around and used by thousands of web developers across the globe. It is licensed and distributed under open source and widely used in the LAMP stack. However, it generally less prevails as compared to Linux in Windows web server environment, but if we want, can easily install PHP and run it on Windows 10 or 11 computers when certain best practices are followed.
To utilize the functionality of PHP running on Windows we need to set up a web server that supports PHP scripts. Some of the popular examples of web server software are Apache HTTP Server, Microsoft IIS (Internet Information Services), and Nginx.
Use Command Prompt or PowerShell to install PHP on Windows
Install the Chocolatey Choco Package manager
I tried Windows default package manager Winget but PHP is not available to install through it. Therefore, the other best option left is to use the popular Chocolatey package manager.
But unlike Winget, Choco is not present on our Windows system by default, hence we need to install it manually on our system.
Go to your Windows 10 or 11 search box and type CMD, as it appears select “Run as administrator“
Copy-paste the given command on our Prompt to install the Chocolatey package manager.
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Restart your system to let the package manager integrate properly.
Command to setup PHP on Windows 10/11
We have the package manager, the next task is to use it to download and install the PHP on Windows. Here is the command to type:
choco install php
For the ThreadSafe version, if you are planning to use PHP with Apache
choco install php --package-parameters='"/ThreadSafe ""/InstallDir:C:\PHP"""'.
Press A and hit the Enter key to allow Choco to install all the packages needed to set up PHP.
The above command will install the latest version of PHP on Windows, however, those who are interested in some previous version can use the given syntax:
For example to get php7.4
choco install php --version=7.4
For more version history check out the Choco official page.
Well, those who want to downgrade the latest version to some previous one or planning to install multiple versions of PHP on Windows 10 or 11, need to define a parameter with the Choco command.
To downgrade the latest version to some older ones. Let’s say you already have 8.2 but now want to install the 7.4 version of PHP, then the command parameter to add will be
choco install php --version=7.4 --allow-downgrade
To install multiple versions of PHP side-by-side:
If you don’t want to remove the current latest version from your PC but want to have some older version installed as well, then use the
choco install php --version=7.4 --side-by-side
Check the PHP version Windows command
After downloading and installing the PHP, to confirm it has been configured properly on Windows 10 or 11, we can run
php -v to check its version and other details.
To check available or installed PHP extensions, use:
Php Info page in Windows
Another way to check the version of PHP on Windows 11 or 10 is by using a single line of code
phpinfo(); that will display all the necessary information including extensions and their versions.
For that, on your command prompt or terminal type:
After that paste the following code and save and close the file.
<?php phpinfo(); ?>
Now, if you may not know PHP comes with its own web server that we can use to test our PHP based applications. So, in the same directory where you have created the info.php run:
php -S localhost:8000
You will see the PHP’s inbuilt web server will be started on your current Windows command prompt. Now, open your browser and type:
And you will see detailed information about your installed PHP version.