There are two methods to switch php versions on the command line ubuntu.
Method 1
Run each command one by one and input the number of the php version you want to change.
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo service apache2 restart
Finally, you must run the apache2 restart command.
Method 2
if you want to change PHP 5.6 to 7.4 just run the below codes.
sudo a2dismod php5.6
sudo a2enmod php7.4
sudo service apache2 restart
if you want to change from PHP 7.4 to 5.6 run the below codes.
sudo a2dismod php7.4
sudo a2enmod php5.6
sudo service apache2 restart
Every time you change PHP versions you need to run sudo service apache2 restart
command.
If you want to know your current PHP version just run php -v