I am working on various PHP-based projects that have different version requirements, for both work and side projects. Homebrew is actually great for this as it allows you to install more than one version of PHP on MacOS. You can then to switch the PHP version easily via brew unlink
and brew link
but over time, this can become tedious.
Quick note: We can use shivammathur/homebrew-php to install any PHP older than 8.0.
So naturally, we can create a utility function to do this. Bonus points:
- It checks against installed PHP version (defined manually in the
installed_php_versions
array). - It skips switching if the version we want to switch is already active.
Put this in the .zshrc
file and reload your terminal, and we are good to go. To use this, simply run switchphp <php_version>
for example switchphp 8.2
.
|
|
Hope this is useful!