Skip to content
zefıly
← All posts

PHP 7.4 to 8.x: a migration checklist that goes one version at a time

6 min read

Upgrading an old PHP 7.4 application straight to the newest PHP rarely works. A step-by-step checklist, with what breaks at 8.0, 8.1, 8.2, 8.4 and 8.5, the tools that find the problems for you, and how to test without touching the live site.

PHP 7.4 stopped receiving security fixes from the PHP project in November 2022. Many applications still run on it, because the upgrade looks like one big, risky jump.

It does not have to be one jump. Each PHP release breaks a small, known set of things. If you go one version at a time, you fix a handful of problems per step and always know which step caused them.

In short

  • Work on a copy of the site, never on the live one.
  • Go 7.4 → 8.0 → 8.1 → 8.2 → 8.3 → 8.4 → 8.5, and stop at each step until the logs are quiet.
  • 8.0 is the big one: it removes old functions and turns many warnings into errors.
  • 8.1 to 8.5 mostly add deprecations: the code runs, but tells you what will break in PHP 9.
  • Let tools do the searching: PHPCompatibility, Rector, PHPStan.

Before you change anything

  1. Make a copy. Clone the site and its database to a staging address. You need a place where breaking things costs nothing.

  2. Turn on full error logging on the copy, to a file, not to the screen:

    error_reporting = E_ALL
    display_errors = Off
    log_errors = On
    
  3. Clean up on 7.4 first. Open every important page and run every cron job, then read the log. Notices and deprecations that PHP 7.4 already reports are the things PHP 8 will refuse. Fixing them now is the cheapest part of the whole upgrade.

  4. Update what is not yours. Framework, CMS, plugins, themes, Composer packages: move each to the newest release that still supports 7.4. Often that release already supports 8.x, and half of the work is done.

  5. Write down what "works" means: the ten pages and actions that must work (log in, check out, send the contact form, generate a PDF, run the import). You will repeat this list at every step.

Step 1: PHP 8.0, the big one

Things that were removed and now stop the script:

  • create_function(), each(), money_format(), get_magic_quotes_gpc().
  • Curly braces for string and array offsets: $str{0} must become $str[0].
  • The (real) cast and the (unset) cast.
  • A method with the same name as its class is no longer a constructor. Use __construct().

Things that changed behaviour:

  • Comparing strings with numbers. 0 == "abc" was true in PHP 7 and is false in PHP 8. Code like if ($status == 0) with a string in $status now takes the other branch, without any error message. Search for loose comparisons on values that come from forms or the database.
  • Wrong types to built-in functions throw TypeError, where they used to give a warning and return null. For example strlen([]).
  • Many warnings became exceptions: division by zero throws DivisionByZeroError.
  • Undefined variables and array keys are warnings now, not notices. If your error handler turns warnings into exceptions, old sloppy code starts failing.
  • The @ operator no longer hides fatal errors.

This step takes the most time. Do not move on until the log is clean.

Step 2: PHP 8.1

  • Passing null to built-in functions is deprecated: strlen(null), trim(null), str_replace('a', 'b', null). Old code does this everywhere, usually with a missing form field. Fix it with $value ?? ''.
  • strftime() and gmstrftime() are deprecated. Use date() or IntlDateFormatter.
  • FILTER_SANITIZE_STRING is deprecated. Use htmlspecialchars() where you output the value.
  • mysqli throws exceptions by default. A failed query used to return false; now it throws mysqli_sql_exception. Code that checks if (!$result) never gets there.
  • Classes that implement built-in interfaces (ArrayAccess, Iterator, Countable) get notices about return types. Add the types, or #[\ReturnTypeWillChange] for a while.

Step 3: PHP 8.2

  • Dynamic properties are deprecated. Writing $this->foo = 1 when the class never declared $foo gives a deprecation. Declare the property. For classes you cannot fix yet, #[\AllowDynamicProperties] silences it.
  • utf8_encode() and utf8_decode() are deprecated. Use mb_convert_encoding().
  • "${var}" in strings is deprecated. Write "{$var}".

Step 4: PHP 8.3

A quiet step for most applications. Run your list, read the log, move on.

Step 5: PHP 8.4

  • Implicit nullable parameters are deprecated. function f(string $a = null) must become function f(?string $a = null). Rector fixes this in bulk.
  • Some extensions left PHP core, among them IMAP. If your application reads a mailbox with imap_open(), check whether your system still has the extension for this version. Ubuntu 26.04, for example, has no IMAP package for PHP 8.5.

Step 6: PHP 8.5

We ran these on PHP 8.5:

  • The casts (integer), (boolean), (double) and (binary) are deprecated. Write (int), (bool), (float), (string).
  • The backtick operator is deprecated. Use shell_exec().

Let tools find the problems

  • PHPCompatibility (a ruleset for PHP_CodeSniffer) reads your code and lists what will not work on a given PHP version, without running it:

    phpcs --standard=PHPCompatibility --runtime-set testVersion 8.0 src/
    

    Run it for the next version before each step.

  • Rector rewrites code for you: nullable types, removed functions, old syntax. Review its changes like any other change.

  • PHPStan finds type problems that become TypeErrors on PHP 8. Even its lowest level finds real bugs in old code.

  • Your own log. No tool finds everything. Click through the list from "Before you change anything" at every step.

WordPress and other CMS sites

The core is rarely the problem; plugins and themes are. Update them all on 7.4 first, then switch PHP on the staging copy. For every plugin that breaks, look for an update, then a replacement. A plugin with no release for years is a risk on any PHP version.

Going live

  1. Put the fixed code on the live site while it still runs the old PHP. Code that works on 8.x and 7.4 at once is normal during a migration.
  2. Take a backup.
  3. Switch the PHP version.
  4. Watch the error log for a day.
  5. If it goes wrong, switch the version back. That takes seconds, because the code works on both.

Where to run all this

You need two things: a staging copy of the site, and a server where the old and the new PHP versions run next to each other.

Zefily does both. Clone a site to a staging copy, then change its PHP version on the site's page at each step. It installs PHP 7.3 to 8.5 on current systems, Ubuntu 26.04 included, and each site runs as its own user in its own pool, so the old version cannot reach other sites. See PHP 7.4 on Ubuntu 26.04.

If you set up the server yourself, see Running several PHP versions side by side on Ubuntu 26.04.

Frequently asked questions

Can I upgrade from PHP 7.4 directly to PHP 8.5?

You can, but every problem of six releases appears at once. Going one version at a time on a staging copy shows you which change caused which error.

What is the hardest step when upgrading from PHP 7.4?

PHP 8.0. It removes old functions and syntax, turns many warnings into errors, and changes how strings are compared with numbers.

Why does strlen(null) give a deprecation?

Since PHP 8.1, passing null to a built-in function parameter that does not accept null is deprecated. Pass an empty string instead, for example with $value ?? ''.

What are dynamic properties and why are they deprecated?

Assigning to a property the class never declared. PHP 8.2 deprecates it. Declare the property in the class.

Which tools help with a PHP 8 upgrade?

PHPCompatibility lists incompatible code for a target version, Rector rewrites many cases for you, and PHPStan finds type errors.

See Web stack for changing a site's PHP version in Zefily.

More posts

Black Friday & Cyber Monday

Before you go: lifetime plans are on sale

Pay once for a lifetime plan, or get 30% off the first year of any yearly plan.

Only 100 lifetime plans per plan. When they are gone, they are gone.

See the campaign

Amazing November

See the campaign →