You can only install supported versions of PHP with brew. However, there is the tap shivammathur/php which can be used to install unsupported version of PHP.
brew tap shivammathur/phpbrew install shivammathur/php/php@7.3brew link php@7.3
The first step only needs to be done once. After adding the tap, you can install PHP version 5.6 – 8.2.