The problem
PHP 5 stopped getting security fixes years ago. That means every month the app runs is a month of unpatched bugs, and hosting providers quietly drop old runtimes. The bigger problem is that the code often works and nobody wants to touch it. A rewrite looks clean on a slide but it stalls on the first missing business rule. Meanwhile the app keeps taking orders or paying staff.
The other trap is the framework. Many PHP 5 apps sit on old CodeIgniter, Symfony 2, Laravel 4 or a homegrown router. Bumping PHP under them breaks things in ways that only show up at runtime. A quick version change on production is not a plan. You need to know which code paths run, which libraries are dead, and which extensions the host still compiles.
A third issue is the database driver. mysql_* functions are gone in PHP 7. If the code calls them directly, every query fails. This is the single most common blocker, and it is mechanical to fix but touches many files. Knowing the count before you start changes the schedule.
The last problem is knowledge. The person who wrote the app may be gone. The rules live in the code and in the heads of the people who use it daily. Modernizing without capturing those rules first means you rebuild the wrong thing, faster.
What a good solution looks like
A good plan moves the app to a supported PHP version in small, reversible steps. You keep the same behavior, then improve. The target is usually PHP 8.1 or 8.2, because those still get security patches and match current library versions. You pick the target with your host and your framework in mind, not by chasing the newest number.
The first visible win is a test harness. Even a thin one matters. You record the real requests the app serves, replay them, and compare responses before and after each change. That harness is what makes small steps safe.
You also separate the layers. Database access goes behind a small set of functions or a query builder. Configuration moves out of hardcoded files into environment variables. Error handling goes to a log instead of the screen. None of this needs a new framework on day one.
Finally, a good solution names a rollback point for every step. If a deploy goes wrong, you restore the previous release and the previous PHP version in minutes. The old version stays available until the new one has run in production long enough to trust.
How to build it
Start with an inventory. List every entry point: web routes, cron jobs, CLI scripts, uploads, queue workers and any API the app calls or exposes. Note the PHP version, extensions, database version and web server. Check which third party libraries are pinned and whether they have newer releases. This inventory becomes your map, and it is the first thing a legacy engagement should produce.
Set up a staging copy that matches production closely, including the database. Turn on error reporting and logging at the highest level. Run the app through its main flows and capture the requests and responses. Tools like PHPUnit, Behat or a simple replay script work here. You do not need full coverage. You need the flows that matter: login, checkout, reporting, imports.
Fix the mechanical blockers first. Replace mysql_* calls with PDO or mysqli. Replace each with preg_replace and the /e modifier with preg_replace_callback. Remove calls to split, ereg and each. These changes are safe and can ship on PHP 5 first, which keeps the diff small and lets you test on the old runtime.
Then move to PHP 7.4 as a waypoint. Run the test harness, fix deprecations and fatal errors, and deploy. PHP 7.4 is past end of life, so treat it as a short stop, not a destination. From there, go to PHP 8.1 or 8.2. Expect stricter type checks, changes to string to number comparisons, and warnings on undefined array keys. Fix them one by one with the harness as your referee.
After the runtime is current, upgrade the framework or replace it, one module at a time. The PHP upgrade and the framework upgrade are separate projects. Doing them together doubles the unknowns. Keep the old framework version running on new PHP while you plan the next move.
Risks and how to manage them
The biggest risk is a silent behavior change. PHP 8 compares strings and numbers differently than PHP 5, and loose comparisons can flip a condition. Loose typing hides it until a customer sees the wrong price. Find these by grepping for double equals and adding tests around the affected logic.
Another risk is the database driver and character set. Moving from mysql_* to PDO changes how errors surface and how encoding is handled. Test with real data that includes accented characters and long text. Check that the connection charset is set explicitly.
Hosting is a risk too. Some shared hosts only offer old PHP or a fixed set of extensions. Confirm the target version and extensions are available before you commit to a date. If not, plan a move to a host or container you control, and test that move separately from the code changes.
Finally, watch the third party dependencies. Old libraries may not run on PHP 8 at all. If a library is abandoned, either vendor a patched copy or replace the call sites. Do not upgrade the whole dependency tree in one go. Pin versions, upgrade one library at a time, and keep the harness green.
How to measure success
Track the PHP version in production and the count of known deprecation warnings. Both should move in one direction. A clean error log with no notices on the main flows is a strong signal that the runtime change is done.
Measure the test harness coverage by flow, not by line percentage. You want the important paths covered. Track how long a rollback takes and confirm it works in a drill. That number tells you how safe your next step is.
Watch the error rate and response time on the main endpoints before and after each deploy. A small rise in 500s after a release is a reason to roll back, not to wait. Also watch the support queue: odd bug reports in the week after a deploy often point to a comparison or encoding change.
When the runtime is current, the framework upgrade becomes the next track. If you want outside help to map the risks and sequence the steps, the Legacy modernization sprint from Cloudverg is $8,500/month and covers inventory, recovered business rules, a ranked risk list and a step by step plan with rollback points. It is one option. A capable in house team can run the same playbook.
Frequently asked questions
Can I upgrade PHP 5 to PHP 8 without rewriting the app?
Yes, in most cases. Fix the removed functions first, such as mysql_* and split, then move to PHP 7.4 as a waypoint, then to PHP 8.1 or 8.2. A test harness that replays real requests keeps each step safe.
What breaks first when moving from PHP 5 to PHP 7?
The mysql_* functions are gone, so direct database calls fail. The /e modifier in preg_replace is removed, and split, ereg and each no longer exist. These are mechanical fixes and can often ship on PHP 5 before you change the runtime.
Should I upgrade the framework and PHP at the same time?
No. Do the PHP runtime change first and keep the old framework running on the new PHP. Upgrade or replace the framework as a separate project, one module at a time, so you only debug one set of unknowns.