PHP 8 Upgrade Errors: How to Fix the 8 Most Common Ones
The eight errors that break most sites after a PHP 8 upgrade, what each one means and the exact code fix.
Most sites that break after a PHP 8 upgrade hit the same handful of errors. Old functions were removed, loose type behaviour became strict, and things that used to be silent warnings now stop the page.
Short answer: upgrade on a staging copy, turn on full error reporting, and fix the errors below in order. Fatal errors first, then deprecations. Most small business sites take a day or two, not weeks.
There's a deadline too. PHP 8.1 and older no longer get security fixes, and PHP 8.2's security support ends on 31 December 2026. Sites on old versions also have no official fix for the critical PHP flaws disclosed in October 2026. Aim for PHP 8.3 or 8.4.
Why this problem exists
PHP 8.0 removed functions that had been deprecated for years and made many type mistakes throw errors instead of warnings. PHP 8.1 and 8.2 added more deprecations. Code written for PHP 5 or 7 often relies on exactly those behaviours.
The 8 most common PHP 8 errors and their fixes
1. Call to undefined function mysql_connect()
The old mysql_* functions were removed in PHP 7.0. Switch to PDO (or mysqli):
// Old
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db, $link);
$result = mysql_query("SELECT * FROM users WHERE id = $id");
// New (PDO, with a prepared statement)
$pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8mb4", $user, $pass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([$id]);
$rows = $stmt->fetchAll();
This one is usually the biggest job, and the most important. The old code often has SQL injection holes that prepared statements close.
2. Call to undefined function each()
Removed in PHP 8.0. Replace while (list($k, $v) = each($arr)) with foreach:
foreach ($arr as $k => $v) { /* ... */ }
3. Call to undefined function create_function()
Removed in PHP 8.0. Use an anonymous function:
// Old
$fn = create_function('$a', 'return $a * 2;');
// New
$fn = fn($a) => $a * 2;
4. count(): Argument #1 ($value) must be of type Countable|array, null given
In PHP 8, count() on null or a string is a fatal TypeError. Make sure you always pass an array:
$total = count($items ?? []);
// or
if (is_countable($items)) { $total = count($items); }
5. Unsupported operand types: string + int
PHP 8 throws a TypeError when you do maths on values that aren't numbers, such as an empty string from a form, or an array. Validate and cast first:
$qty = (int) ($_POST['qty'] ?? 0);
$total = $qty + 1;
6. Passing null to parameter #1 ($string) of type string is deprecated
From PHP 8.1, passing null to built-in functions like strlen(), trim() or str_replace() triggers this. It's a deprecation, so it won't break the page yet, but it floods logs and becomes an error in future versions:
$name = trim($_GET['name'] ?? '');
7. Creation of dynamic property Foo::$bar is deprecated
From PHP 8.2, setting a property that isn't declared on the class is deprecated. Declare it:
class Foo {
public ?string $bar = null;
}
For old code you can't refactor yet, add #[\AllowDynamicProperties] above the class as a temporary bridge.
8. Array and string offset access syntax with curly braces is no longer supported
$str{0} is a fatal error in PHP 8.0. Use square brackets: $str[0].
The solution — the Hitori PHP upgrade path
- Clone the site to staging. Same code, a copy of the database, never the live server.
- Scan before switching. Run PHPCompatibility with PHP_CodeSniffer against your code to list the problems up front (command below).
- Let Rector do the repetitive fixes. Rector can rewrite many old patterns automatically. Review every change.
- Update third-party code first: CMS core, plugins, themes, Composer packages. Many errors disappear once they're on current versions.
- Switch staging to PHP 8.3 or 8.4 with
error_reporting(E_ALL)anddisplay_errorson (staging only). - Click through the critical paths: login, forms, checkout, payments, search, admin pages, cron jobs and emails.
- Fix fatal errors, then deprecations, using the list above.
- Switch live, restart PHP-FPM, and watch the error log for 48 hours.
phpcs -p --standard=PHPCompatibility --runtime-set testVersion 8.3 ./
Fix options compared
| Approach | Time | Risk | Best for |
|---|---|---|---|
| Switch PHP version on live and hope | Minutes | High | Never |
| Staging + manual fixes | 1–3 days | Low | Small custom sites, brochure sites |
| Staging + PHPCompatibility + Rector | 2–5 days | Low | Larger custom codebases |
| Rebuild on a modern framework | Weeks | Medium | Very old code with no tests and frequent hacks |
Common mistakes and how to avoid them
- Hiding errors instead of fixing them. Turning
display_errorsoff on live is right. Turning off logging isn't. - Upgrading PHP before plugins and themes. Update them first, while the site still works.
- Skipping cron jobs and background scripts. They break silently because nobody sees the page.
- Stopping at PHP 8.2. It loses security support at the end of 2026. Go to 8.3 or 8.4 and do the work once.
Real-world example
A typical case: an older ecommerce site still on PHP 7.2, with custom code written years ago and a history of hacks. PHPCompatibility usually lists dozens of mysql_* calls, a few each() loops and some create_function() callbacks in old plugins.
The order that works: update the CMS and plugins, replace the mysql_* layer with PDO and prepared statements, fix the remaining fatal errors on staging, then switch to PHP 8.3. That closes the version gap and the SQL injection holes that were probably behind the hacks.
Troubleshooting checklist
- ☐ Staging copy with a copy of the database
- ☐ PHPCompatibility scan run against PHP 8.3
- ☐ CMS, plugins, themes and Composer packages updated
- ☐ All
mysql_*,each()andcreate_function()calls replaced - ☐ Forms, checkout, admin and cron jobs tested
- ☐ Deprecations logged and cleared
- ☐ Live switched, PHP-FPM restarted, logs watched for 48 hours
Frequently asked questions
Which PHP version should I upgrade to in 2026? PHP 8.3 or 8.4. PHP 8.2 only has security fixes until 31 December 2026, and 8.1 and older get none.
Will WordPress work on PHP 8.3? Current WordPress core does. Older plugins and themes are what usually break, so update them first.
Can I just hide the deprecation warnings? On live, yes: don't display them. But keep logging them and fix them, because deprecations become errors in later versions.
How long does a PHP 8 upgrade take? For a typical small business site, one to three days including testing. Very old custom code takes longer.
Get a fixed-price quote
Tell us what's broken or what you need. We reply within one working day with a price and timeline. No obligation.
Don't have time to do it yourself? We'll upgrade your site on staging, fix what breaks and switch you over with minimal downtime. Fill in the form above for a fixed-price quote, or see our PHP security fix service.