You could use a PPA to stay up-to-date with PHP or install previous version of PHP (f.e. PHP 5.6 on Ubuntu 16.04 LTS).
The most widely used repositories come from Ondřej Surý, the Debian PHP maintainer:
- ppa:ondrej/php
(for PHP 5.6/7.0/7.1 co-installable versions, PHP 5.5 is no longer supported)
The php5 compat packages:
(Click here for instructions on using PPAs.)
PHP PPAs previously contained Apache 2.4 update. This is no longer a case, you need to add separate Apache 2.4 repository:
If you want use these PPAs, do this:
ppa:ondrej/php
(for PHP 5.6/7.0/7.1)
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install php7.1 # for PHP 7.1
sudo apt-get install php7.0 # for PHP 7.0
sudo apt-get install php5.6 # for PHP 5.6
To switch between installed versions use
sudo update-alternatives --config php
Then you must set Apache to work with right version:
sudo a2dismod php7.1 # unload the current version
sudo a2enmod php5.6 # load the version you need
sudo service apache2 restart # restart webserver to apply
ppa:ondrej/php5-compat
(for php5 compat)
sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/php5-compat
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install php5 # this will pull php5.6 package
If you don't have add-apt-repository
binary do the following:
sudo apt-get install python-software-properties
Precautions:
PHP 5.4
Prepackaged latest PHP 5.4 now resides in separate PPA:
ppa:ondrej/php5-oldstable 
Please be aware that PHP 5.4 has reached its end of life on 3. September 2015 and it doesn't receive any security updates. It's recommended to migrate to at least PHP 5.6 that will receive security updated till 31. December 2018.