When a new version of a technology is released, we tend to compare what has changed compared to the previous version. This has happened quite a bit with PHP, which had its version 8 launched at the end of last year.
To carry out the comparison of the changes in the new version, we can install two versions of the language. We have already shown how to do this with Ubuntu. You can access this content by clicking here.
In this article, we will talk again about how to install two versions of PHP, but now on Windows.
Downloading the two versions
We will first download PHP version 7.4. For this, we will use the following link: https://windows.php.net/downloads/releases/php-7.4.13-nts-Win32-vc15-x64.zip
The downloaded file will be zipped. We will extract it to the path C:/tools/php/php7.
Next, we will download PHP 8 via this other link: https://windows.php.net/downloads/releases/php-8.0.0-nts-Win32-vs16-x64.zip
We will extract this file to a similar path, changing only the final folder to php8.
We will now start the configuration of both versions, which will be the same for both. This procedure can be seen in more detail in the article PHP 8 on Windows 10 with Nginx server.
Configuring the php.ini file
We will rename the file php.ini-development to php.ini. We will make some changes to this file.
The first directive we will change is error_reporting:
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
The next directive is doc_root:
doc_root = "C:\www"
Next, we will change the extension_dir and sys_temp_dir directives:
extension_dir = "ext"
sys_temp_dir = "C:\Windows\Temp"
Finally, we will enable some PHP extensions:
extension=curl
extension=fileinfo
extension=mbstring
extension=exif
extension=mysqli
extension=openssl
extension=pdo_mysql
Adding PHP to the system PATH
The next step involves adding the PHP executable to the system variables. This will make it possible for us to run PHP through its native command line and allow us to execute it in any folder on the computer.
We will access the folder where we extracted the PHP 7 files and rename the php.exe file to php7.exe:
We will repeat the same process for the PHP 8 folder:
We will now actually add these files to the system variables.
In Windows Explorer, find the item “This Computer” on the left panel, right-click on it and select the “Properties” option:
In the window that will open, we will select the “Advanced system settings” option:
In the next window, we will click on “Environment Variables”:
Next, we will see another window that will show the user variables and also the system variables. We will edit this second option. Select the “Path” variable, which will turn blue, and click on the “Edit” button:
In the next window, click the “New” button located at the upper right corner. A new space in the Path variables will open. There, you can paste the path to the PHP 7 folder. In our case, this path is C:\tools\php\php7. Finally, just repeat this same process for the PHP 8 folder. The list of folders should look like this:
We can close all the windows to perform our first test with these versions.
Testing the two versions of PHP
To carry out our first test, we will create a PHP file and insert the phpinfo() function into its structure.
We will open a Terminal in the folder where this file was created and execute the following command:
php7 -S localhost:80
Note that “php7” is the name of the executable file we renamed.
Now, when accessing localhost in our browser via port 80, which is the default, we see the following result:
Now, let's open another Terminal in the same folder and start a new PHP server, but now using another port, like 81. To do this, we will use the following command:
php8 -S localhost:81
Now, when we access the address localhost:81 in the browser, we see the following result:
Impressive! We were able to see the information of PHP 8. The most interesting thing is that localhost on port 80 continues to work normally. Are they running separately?
Conclusion
In this article, we learned how to configure two versions of PHP on Windows. We are now prepared to test the differences between the two versions of the language through the native PHP server, carrying out tests for versions 7 and 8 in the same file.
Remember that you can learn all the new features of PHP in version 8 in our course PHP 8 - The Language. ?
Until the next article :).