
The way that the command line shell knows what you mean when you type php
is to lookin a number of defined directories in your computers hard drive, this is known as your $PATH
or path.
Homebrew automatically installs software into a directory of it’s own that isn’t in the path, and then uses symlinks to make them available in the path.
When you first installed PHP it would have been the earlier 7.x version you are ‘stuck’ with, this can be removed from the path using brew unlink php@7.4.33
.
Once this is complete you can then issue brew link php@8.1.12
, according to the aricle I am referencing this might require a forced overwrite, for example: brew link php@8.1.12 --force --overwrite
.
You should now be able to close the console and reopen it, then enter php -v
and it should now be the newer version.
Source: https://localheinz.com/articles/2020/05/05/switching-between-php-versions-when-using-homebrew/