Created
January 15, 2024 02:21
-
-
Save mynameismaxz/a1bba0c9b40ebee6ec35a3ca4da40448 to your computer and use it in GitHub Desktop.
PHP Switch version on MacOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if Homebrew is installed | |
if ! command -v brew &>/dev/null; then | |
echo "Homebrew is not installed. Please install Homebrew first." | |
exit 1 | |
fi | |
# Check if PHP 7.1 and PHP 8 are installed via Homebrew | |
if ! brew ls --versions php@7.1 &>/dev/null; then | |
echo "PHP 7.1 is not installed via Homebrew. Please install it first." | |
exit 1 | |
fi | |
if ! brew ls --versions php &>/dev/null; then | |
echo "PHP 8 is not installed via Homebrew. Please install it first." | |
exit 1 | |
fi | |
# Check if a PHP version argument is provided | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <php-version>" | |
echo "Available PHP versions: 7.1, 8" | |
exit 1 | |
fi | |
# Determine the selected PHP version | |
selected_version="$1" | |
case "$selected_version" in | |
7.1) | |
echo "Switching to PHP 7.1..." | |
brew unlink php | |
brew link --force --overwrite shivammathur/php/php@7.1 | |
;; | |
8) | |
echo "Switching to PHP 8..." | |
brew unlink php@7.1 | |
brew link --force --overwrite php | |
;; | |
*) | |
echo "Invalid PHP version. Available options: 7.1, 8" | |
exit 1 | |
;; | |
esac | |
# Verify the switch | |
if [ $? -eq 0 ]; then | |
echo "PHP version switched to $selected_version." | |
else | |
echo "Failed to switch PHP version to $selected_version." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment