Upgrading a PHP 5 Application to PHP 8: Possible Without a Rewrite?

Sandor Farkas
Founder & Lead Developer
Expert in software development and legacy code optimization
LinkedInSomewhere in your company there is probably a PHP application that nobody wants to touch. It handles orders, quotes or internal approvals, it was written ten or fifteen years ago, and it still runs on PHP 5.6 because every attempt to move it ended with a white screen. When someone finally asks for a PHP 5 to PHP 8 upgrade, the first reaction from developers is often "we should just rewrite it."
Sometimes that is the right call. More often it is not. In our assessments of old PHP codebases, most applications can be brought to a current PHP version step by step, while the business keeps using them. The work is tedious and needs discipline, but it carries far less risk than replacing a system whose business rules exist only in the code.
This guide covers the extreme case: an application that starts on PHP 5.x. If you are already on PHP 7.4, our guide on upgrading PHP 7.4 to PHP 8.3 is the better starting point.
Why is a PHP 5 application a real risk today?
Because nobody has fixed security bugs in it for years. According to the official PHP end-of-life list, PHP 5.6 reached its end of life on 31 December 2018 and PHP 7.0 followed on 10 January 2019. Every vulnerability found in the runtime since then stays open on your server, unless your Linux distribution still backports fixes for that PHP branch, which is worth checking before you rely on it.
The runtime is only half of the problem. A PHP 5 application usually runs on an old operating system with an old web server, old OpenSSL and an old database driver, because newer versions of those components no longer support PHP 5. The whole stack is frozen. We wrote about what that means in practice in the risk of running unsupported PHP.
What usually blocks the upgrade?
A handful of removed features cause most of the breakage. PHP 7.0 alone removed several extensions that old applications depend on heavily. The PHP 7.0 list of removed extensions includes ereg and the original mysql extension, so every mysql_query(), mysql_fetch_assoc() and ereg_replace() call stops working.
The usual suspects, roughly in the order we meet them:
- Database access through the
mysql_*functions. These have to move to PDO or mysqli. Because the old functions often build SQL by string concatenation, this is also the moment where SQL injection holes become visible. - Regular expressions through
ereg()anderegi(), which need to be rewritten aspreg_*calls with adjusted patterns. - Encryption through
mcrypt. The PHP 7.2 migration notes describe how the extension was moved out of core into PECL. Data encrypted with mcrypt has to be decrypted and re-encrypted with OpenSSL or sodium, so the code change comes with a data migration attached. - Removed language features. The PHP 7.0 incompatibility list drops ASP-style tags (
<%) and functions such ascall_user_method()andset_magic_quotes_runtime(). The PHP 8.0 incompatibility list removeseach(),create_function()andget_magic_quotes_gpc(). - Behaviour changes that do not throw errors. PHP 8.0 changed how non-strict comparisons between numbers and non-numeric strings work (see the same PHP 8.0 list). Code like
if ($status == 0)can quietly start taking a different branch. - An old framework. Applications built on early Zend Framework, CodeIgniter 2 or a custom framework from 2009 often depend on all of the above at once.
The silent changes are the dangerous ones. A missing function produces a fatal error that you find on the first request. A changed comparison produces a wrong invoice three weeks later.
There is also a category the PHP manual cannot help with: patterns from the register_globals era. Code that reads $id directly instead of $_GET['id'], includes files based on request parameters, or relies on magic quotes for "escaping" needs to be found and fixed by hand. In audits we regularly find these spots clustered in the oldest admin screens that nobody has opened in years.
Can you jump from PHP 5.6 straight to PHP 8?
You can try, but we do not recommend it. Going directly means fixing two major versions of breaking changes at the same time, and when something fails you cannot tell which change caused it.
The path that works is staged:
- PHP 5.6 to PHP 7.4. This is the big step. Removed extensions, the new error model and most of the legacy syntax are handled here. PHP 7.4 is also the last version where much old code still runs with deprecation notices instead of fatal errors, which gives you a clear list of what to fix next.
- PHP 7.4 to PHP 8.x. With a clean 7.4 codebase, this step follows the well-documented route from our 7.4 to 8.3 guide.
- On to a currently supported version. The supported versions page shows which branches still get security fixes. Aim for one that has at least two years of support left.
Each stage ends with the application running in production on the new version. That gives you real usage as a test and lets you stop between stages if priorities change, without a half-finished branch rotting in the repository.
Which tools do the heavy lifting?
Two open-source tools cover a large part of the mechanical work. PHPCompatibility is a ruleset for PHP_CodeSniffer that reports code using features removed or changed in a target version. You run it with a testVersion setting, for example 7.4 first and later 8.3, and it gives you a concrete list of files and lines.
Rector then fixes much of that list automatically. Its maintainers state that it supports upgrades from PHP 5.3 up to 8.5. It rewrites each() loops, replaces create_function() with closures and adds many of the small syntax changes. We describe how we use it in Rector for legacy PHP.
Neither tool replaces understanding the code. Rector will not migrate your mcrypt-encrypted customer data, and it cannot know that $status == 0 was meant to match the string '0' and nothing else. Those decisions need a developer who reads the code and, ideally, someone from the business who knows what the output should be.
How do you keep the old system safe during the upgrade?
You contain it, because a staged upgrade takes weeks or months and the application stays exposed the whole time. Containment leaves the vulnerabilities in place, but it shrinks the attack surface while the real work happens.
What we usually put in place first:
- Run the application in an isolated container with only the network access it needs. The old PHP runtime then no longer shares a server with anything else, and moving to a new host becomes a configuration change. Our post on moving a shared hosting PHP app to modern infrastructure covers the details.
- Put a web application firewall or at least a reverse proxy with strict rules in front of it. Block admin paths from the public internet if users can reach them through a VPN instead.
- Add basic monitoring and error logging before changing any code. You want to know what "normal" looks like so you can spot regressions after each stage.
- Write characterization tests for the processes that make money. They record what the code does today, right or wrong, so you can confirm it still does the same after each stage. A few dozen tests around order creation, pricing and invoicing catch most of the silent comparison bugs.
In older codebases we often find zero automated tests. That is normal, and it is the main reason to write these tests before touching the PHP version, not after.
How long does a PHP 5 to PHP 8 upgrade take?
There is no honest single number, because the effort depends far more on the code than on its size. Two applications with the same line count can differ by a factor of several in effort. What drives it:
- how much database access goes through raw
mysql_*calls spread across templates, versus a single data layer, - whether a framework is involved and whether that framework has a supported successor,
- whether encrypted data has to be migrated,
- how many external integrations (payment, ERP, shipping) need testing against the new stack,
- and whether anyone still knows what the application is supposed to do.
A short assessment before committing to a budget pays for itself. Running PHPCompatibility against the codebase for a day already shows how many breaking calls exist and where they sit. If you want a structured view, our legacy code optimization work starts with exactly that inventory.
When is a rebuild actually cheaper?
A rebuild wins when the upgrade would change most of the code anyway. We recommend considering it seriously in these situations:
- The framework is dead and has no upgrade path, so you would be rewriting the framework layer by hand.
- Business logic, HTML and SQL are mixed in the same files throughout, so every fix touches all three.
- The business process itself has changed so much that large parts of the application are unused or worked around in spreadsheets.
- The application is small enough that a rebuild fits in a few weeks and the requirements are well understood.
Even then, a big-bang replacement is rarely the safest option. The strangler fig pattern lets you replace one area at a time while the old system keeps running, which often means containing and partially upgrading the old application first anyway. For a deeper decision framework, see when to rewrite vs refactor.
If you are unsure whether to upgrade at all, or whether paid long-term support could buy you time, our comparison of upgrading versus extended support walks through the options.
A realistic first step
You do not need to decide between upgrade and rebuild today. Start with an inventory: which PHP version runs where, which removed functions the code uses, which data is encrypted with what, and which business processes depend on the application. With that list, the decision usually becomes obvious.
If you would like a second pair of eyes on that inventory, or want help planning the staged upgrade, write to us at hello@wolf-tech.io or have a look at wolf-tech.io. We have taken old PHP applications through this path before, and we can usually tell within a short code quality review whether yours is an upgrade or a rebuild candidate.
