Posted by:
unfun
(---.184.189.46.rev.vodafone.pt)
Hello all!
Another way is to add to path, yes, but change the contents of that same folder to the desired php.
Sounds crazy, wait for it.
In my case, and I think of most who code in PHP, the terminals we SSH to are usually linux bash, so I prefer to do my development with Git Bash for windows to emulate linux terminal, so the following code applies to windows users with Git Bash mainly, but same principle can be used in command prompt.
To briefly explain my code, I create a symbolic link to the desired PHP version in "c:\scripts" called "www" so then I can safely add "C:\Scripts\www" to my path variable.
For this user only has to set up their own settings, like WAMP folder and default PHP version, when user wants to change version in CLI, will read PHP sub-folders to see what versions you have installed, stick those in a numeric array and then loop through this array and show user installed versions, when user selects one, it will delete the "www" current symbolic link in c:\scripts and create a new one pointing to chosen PHP desired version's exe.
It works a treat to me, then I just added an alias to MySQL so I can create my project and my DB straight at once.
Here's how i done it:
For git bash, jsut create a file called .bashrc in your user's folder with the following and then adjust to your own needs. Fairly Readable code, I like to think, hope it is useful for others.
Here it goes:
#!/bin/bash
default_php=php8.1.0;
default_mysql=mysql5.7.36;
php_folder=/c/wamp64/bin/php/;
mysql_folder=/c/wamp64/bin/mysql/;
current_folder=$PWD;
main_link_folder=/c/Scripts;
link_php=www;
# In this case I would add C:\Scripts\www to my PATH variable
echo '____________________________________';
echo "PHP CLI Version: ${default_php//[!.0-9]/}, to change press P";
echo "MySQL CLI Version: ${default_mysql//[!.0-9]/}, to change press M";
echo '____________________________________';
while : ; do
read -s -n 1 k <&1
if [[ $k = p ]] ; then
for entry in "$php_folder"/*
do
if [[ ! $entry == *.txt ]]; then
arrVar[${#arrVar[@]}]=$(basename $entry)
fi
done
for i in "${!arrVar[@]}"
do
echo "$i : PHP ${arrVar[$i]//[!.0-9]/}"
done
read -s -n 1 opt <&1
if [ "${arrVar[opt]+abc}" ]; then
cd $main_link_folder
rm -rf $link_php
ln -rsfT $php_folder${arrVar[$opt]} $link_php
cd $current_folder
alias mysql="winpty $mysql_folder$default_mysql/bin/mysql.exe"
return
else
cd $main_link_folder
rm -rf $link_php
ln -rsfT $php_folder$default_php $link_php
cd $current_folder
alias mysql="winpty $mysql_folder$default_mysql/bin/mysql.exe"
return
fi
else
cd $main_link_folder
rm -rf $link_php
ln -rsfT $php_folder$default_php $link_php
cd $current_folder
alias mysql="winpty $mysql_folder$default_mysql/bin/mysql.exe"
return
fi
done
break
Edited 2 time(s). Last edit at 03/19/2022 03:25AM by unfun.