MAMP comes bundled with several PHP versions, and you can choose the recent two from the UI.
You can check the installed PHP version at location /Applications/MAMP/bin/php.

If the required version is not in the /Applications/MAMP/bin/php folder, first, you need to install the version. In my case, PHP version 8.1.10 is already installed on the system, which I want to use with MAMP.

Once the version is installed on your machine, follow the below steps to change the PHP version on MAMP.
Step 1: Navigate to the location where PHP is installed. In my case it is /usr/local/Cellar/php.
Step 2: Next we need to copy the PHP folder 8.1.10_1. Open the location from UI. Click on Go -> Go to Finder and paste the path /usr/local/Cellar/php.
Copy the folder and paste it to /Applications/MAMP/bin/php. To do it using command line use
cp -r 8.1.10_1 /Applications/MAMP/bin/php/
Step 3: Rename the folder you pasted by following the naming convention used by the existing folders. Otherwise, MAMP will not detect your version.

I have renamed folder 8.1.10_1 to php8.1.10.
Step 4: Restart MAMP and check for the latest version is showing. You will get an error if you try to switch to the PHP version in MAMP and start server.
You can get more detail about the error by opening the apachectl file located at the path /Applications/MAMP/Library/bin

We are getting the error at line number 160 in httpd.conf file because MAMP is looking for file libphp.so which is not avilable at path
/Applications/MAMP/bin/php/php8.1.10/modules/libphp.so
You will find libphp.so file in /Applications/MAMP/bin/php/php8.1.10/lib/httpd/modules/ or /usr/local/lib/httpd/modules/
Step 5: Copy modules folder containing libphp.so file to php8.1.10 folder so that file is available at path MAMP is looking for.
Step 6: Restart MAMP. This time you will not get any errors.