Quick Snipp: Switch PHP Versions

Recently had to look after some old PHP projects. Some these project don’t work well with the PHP 7 so I had to constantly switch between the different versions.

So if you are working on a linux environment(WSL in my case) then you can use this small script to quickly switch between PHP 5 and 7.

if [ $1 = "5" ]; then
    sudo a2dismod php7.2
    sudo a2enmod php5.6
    sudo service apache2 restart
    sudo update-alternatives --set php /usr/bin/php5.6
fi
if [ $1 = "7" ]; then
    sudo a2dismod php5.6
    sudo a2enmod php7.2
    sudo service apache2 restart
    sudo update-alternatives --set php /usr/bin/php7.2
fi

Save this with any name that suits you like e.g switch-php.sh,

remember to make it executable chmod u+x switch-php.sh

and then use it like ./switch-php.sh <7|5>

Do have a look at this nice article, to get more information about installing multiple PHP versions.

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *