Updating is never 100% painless, but the team behind PHP has tried its best to lower the incompatibilities and provide a good experience during the process of updating from PHP 5 to 7.
Migration tools
Before moving your application from PHP 5 to 7, you should most definitively back up. You don’t want to lose your work. You can also use various migration tools that can tell you in advance what part of the code most probably won’t work and how to fix it. Check one of these 3:
PHP 7 MAR
php7mar is a simple tool that generates reports. It will show which line of code has a problem, put a note and suggest a course of action.
PHP 7 Compatibility Checker
php7cc is another similar tool for compatibility checking. It will show errors in red, which could be fatal, syntax, or notices. It will also display warnings in yellow.
PHPto7aid
php7aid is showing directly what part of your PHP 5 code won’t work. It will try to help you resolve the problems.
Updating PHP 5 to PHP 7 on Linux
In this step-by-step guide, we will update PHP 5 to PHP 7 on an Ubuntu computer running LEMP (Linux, Nginx, Mysql, PHP) and a user with sudo permissions.
Before installing or updating, we need to add the repository, which contains PHP 7 for Ubuntu. Use the following command:
sudo add-apt-repository ppa:ondrej/php
The Terminal will show you a description of the repository. Press Enter and continue.
Then you will need to check for updates with this command:
sudo apt-get update
Finally, we can install PHP 7 with the following command:
sudo apt-get install php7.0-fpm php7.0-mysql
After the installation of the PHP 7, change the path to the fastcgi_pass to the new PHP 7. You need to change “/var/run/php5-fpm.sock” to “/var/run/php/php7.0-fpm.sock”. The result should look like this:
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Now that we are ready, restart the Nginx:
sudo service nginx restart
Ready!
How to use PHP code in Linux command line?
Updating PHP 5 to PHP 7 on macOS
To update PHP 5 to 7 on macOS, we are going to use Homebrew. If you don’t have Homebrew installed already, you can follow these steps to get it.
Open the Terminal application.
Paste the following command onto the Terminal and press Enter:
/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)”
Press Enter again to agree and then enter your username and password.
Now you are ready to use Homebrew.
Again, inside the Terminal, you will need to use a few commands:
brew install openldap libiconv
brew tap exolnet/homebrew-deprecated
brew unlink php56
brew install [email protected]
There are no older versions left inside Homebrew’s repositories, so here we are installing directly PHP 7.4 that is still available. In this case, we have unlinked an older version PHP 5.6, before installing the new one. If you have another previous version, you can unlink it.
Updating PHP 5 to PHP 7 on Windows
If you are a Windows user and you are using XAMPP or WAMP, just update your software. The newer version will have the latest PHP version. You can also go to their websites and download the right version for you. There you can see with which PHP version they are coming before downloading and installing them.
Install PHP on Windows 10 – Step by step
Updating on CentOS, Fedora, Red Hat
For those of you running any of these OSes, you will need to follow these commands:
sudo yum update
rpm – Uvh https://dl.fedoraproject.org/pub/epel/epel/epel-release-latest-7.noarch.rpm
rpm – Uvh https://mirror.webtatic.com/yum/e17/webtatic-release.rpm
sudo yum install php70w
sudo yum install php70w-mysql
Consider updating from PHP 5 to PHP 8 directly.
There will be support for PHP 7.3 until 06.12.2021 and PHP 7.4 until 28.11.2022. Consider making the jump directly to PHP 8 so you can get the newest features and longer support.