Install multiple versions of PHP on MacOS 12 (Monterey)

PHP is an excellent programming language that comes pre-installed in the latest versions of MacOS. However, only the latest available version will be installed. The good news is that in MacOS we can easily switch PHP versions, today we will see how to make this possible.
Detect the current installed version
To detect which versions we have installed we can first perform a search with brew.
~ brew search php
==> Formulae
brew-php-switcher php-cs-fixer php@7.3 phpbrew phpmyadmin pcp
php ✔ php-cs-fixer@2 php@7.4 phplint phpstan pup
php-code-sniffer php@7.2 php@8.0 phpmd phpunit
==> Casks
eclipse-php phpstorm
Which indicates that the only installed version is as follows: php ✔. If you have more installed versions, they will all appear with the corresponding check.
To see which version we have enabled by default, we can run the following command.
~ php -v
PHP 8.1.0 (cli) (built: Nov 28 2021 01:31:19) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.0, Copyright (c) Zend Technologies
with Zend OPcache v8.1.0, Copyright (c), by Zend Technologies
Enable unsupported versions
To enable previous versions that are not available in brew, you can do so by running the following command.
brew tap exolnet/homebrew-deprecated
In this case, in this way you can install any of the following versions of PHP.
brew install php@5.6
brew install php@7.0
brew install php@7.1
brew install php@7.2
brew install php@7.3
brew install php@7.4
Install a new version of PHP
For this example, we are going to install version 7.4 of PHP. However, you should be able to install the version you require according to your needs.
brew install php@7.4
When you install the version you need, you will see a summary that indicates how you should configure apache so it can interpret PHP code.
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Finally, check DirectoryIndex includes index.php
DirectoryIndex index.php index.html
The php.ini and php-fpm.ini file can be found in:
/opt/homebrew/etc/php/7.4/
php@7.4 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.
If you need to have php@7.4 first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc
For compilers to find php@7.4 you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/php@7.4/lib"
export CPPFLAGS="-I/opt/homebrew/opt/php@7.4/include"
To restart php@7.4 after an upgrade:
brew services restart php@7.4
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/php@7.4/sbin/php-fpm --nodaemonize
Switch between PHP versions
You must keep in mind that after installing a new version of PHP, it will not be automatically enabled. To do this, you must first detect which version you have installed (previous step) and link the new version. In this case, as the current version is the default version (php ✔), just execute the following commands.
brew unlink php
brew link php@7.4 --force
When you check the PHP version, you will see that it corresponds to the one we just changed.
~ php -v
PHP 7.4.26 (cli) (built: Nov 28 2021 16:40:00) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.26, Copyright (c), by Zend Technologies