Skip to content

PHP

Upgrading PHP 7 to PHP 8: Common Errors and How to Fix Them

The PHP 8 changes that break older code most often, from removed functions to stricter type errors, and a safe way to upgrade.

By Hit Infotech Team 3 min read

Upgrading PHP 7 to PHP 8: Common Errors and How to Fix Them

Your hosting company announces that PHP 7 is being switched off, you change the version, and the site shows a blank page or a fatal error. It is one of the most common reasons people call us. The fixes are usually small, but there can be many of them, so it pays to know what to look for.

Errors that appear after switching to PHP 8.0

  • Removed functions: each(), create_function(), money_format() and get_magic_quotes_gpc() no longer exist and cause a fatal error. Replace them with foreach loops, closures and NumberFormatter.
  • Curly brace string offsets: $text{0} is a fatal error. Use $text[0].
  • Stricter types for built-in functions: many now throw a TypeError instead of a warning. For example, count() on null or a string throws an error.
  • String and number comparison: 0 == "abc" is now false. Code that relied on the old loose comparison can quietly change behaviour.
  • New reserved words: match and mixed are reserved, so classes or functions with those names must be renamed.
  • Warnings that used to be notices: reading an undefined array key or a property of null now raises a warning, which can fill logs or break output in older code.

Deprecations in PHP 8.1 and 8.2

  • PHP 8.1: passing null to built-in functions that expect a string, such as strlen(null) or str_replace() with null, is deprecated.
  • PHP 8.2: creating dynamic properties on a class that does not declare them is deprecated.
  • PHP 8.2: utf8_encode() and utf8_decode() are deprecated. Use mb_convert_encoding().

Deprecations do not break the site today, but they become errors in a later major version. Fixing them during the upgrade saves a second round of work.

A safe way to upgrade

  1. Make a staging copy of the site and set it to the target PHP version.
  2. Scan the code with PHP_CodeSniffer and the PHPCompatibility standard to find most problems before running anything.
  3. Use Rector to apply the safe, mechanical fixes automatically, and review the changes.
  4. Update Composer packages, CMS plugins and extensions to versions that support the new PHP version.
  5. Turn on full error reporting on staging, click through every important page and form, and fix what shows up in the log.
  6. Switch the live site only when staging runs without errors, and keep the old PHP version available for a quick rollback.

Which PHP version should you move to?

Move to a version that still receives security updates, and check the supported versions page on php.net. At the time of writing, PHP 8.3 and 8.4 are good targets for most business sites. Older frameworks or plugins may limit you, which is one more reason to update them during the upgrade.

How we help

We have been writing PHP for business and e-commerce applications since 2014, and PHP 7 to 8 upgrades are a regular part of our work. We do a short audit and a written plan before quoting, work on staging, not on your live site, and test after every change.

Need help with this?

PHP bug fixing & PHP 8 upgrades

See the service

FAQ

Frequently asked questions

Usually a fatal error from a removed function or a stricter type check. Turn on error logging on a staging copy to see the exact error and the file it comes from.

Have a store or app that needs work?

Send us your URL or idea. We take a first look for free and reply with what we would do and what it would cost.