Assuming that you have installed php from brew. You can switch the versions between php from the script below.

Update the script as per the requirement of your versioning.

#!/usr/bin/python3

""" 
script to switch between php versions
author: msamgan
created: 22 july, 2022
additional info
    - make the script executable chmod +x php.switch <version>
    - put the script in .local/bin for global access
    - or run directly from terminal ./php.switch <version>
"""

import argparse
import os
import sys

parser = argparse.ArgumentParser(description="Script to switch between php versions")
parser.add_argument("version", metavar="Version", type=str, help="Version")

args = parser.parse_args()

print("Switching php version to: " + args.version)

if args.version == "8.1":
    os.system("brew unlink [email protected] && brew link [email protected] --force --overwrite")
    os.system("php -v")
    sys.exit()

if args.version == "7.4":
    os.system("brew unlink [email protected] && brew link [email protected] --force --overwrite")
    os.system("php -v")
    sys.exit()

print("Invalid version")