Source: andrej 2021-02-26 13:38:56
- Have a directory for the user for shell scripts:
mkdir ~/bin
- Have this directory in the shell's PATH. Edit
~/.bash_profile
- and add:
[ -d ~/bin ] && { PATH="${HOME}/bin:${PATH}"; }
export PATH
- Then manually symlink the correct php into ~/bin/php. Or you can put the following script into
~/bin/usephp
#!/bin/sh
[ "$#" -lt "1" ] && {
echo "Configure current shell to use a specific major PHP version.
Usage: usephp 7"
exit 0
}
binary="php${1}"
wanted="/usr/bin/${binary}"
[ ! -f "${wanted}" ] && {
echo "'${binary}' not found. Using 'php'."
wanted="/usr/bin/php"
}
ln -sf "${wanted}" "${HOME}/bin/php"
inUse=$(php -nr "echo phpversion();")
echo "Shell configured to use PHP ${inUse}."
- Make it executable
chmod u+x ~/bin/usephp
- and use it like so: use php version 8
usephp 8
Output:
php -v PHP 8.0.2 (cli) (built: Feb 2 2021 18:26:02) ( NTS )
use php version 7
usephp 7
Output:
php -v PHP 7.4.15 (cli) (built: Feb 2 2021 18:30:22) ( NTS )
Pages:1