PHP 8 introduced stricter syntax enforcement that silently breaks code written for PHP 7.4. Things the older interpreter accepted without complaint, like unquoted constant names, bare array keys, and unquoted format strings, now throw fatal errors and take your site down. This guide covers the most common causes and how to fix them.
How to Know If PHP Is the Cause
Your site may show a blank page, load only partially, or show a server error. If the timing lines up with a server migration or a PHP version change, that’s your first clue.
Check your PHP error log from the site folder. If you see lines like these, PHP version is the cause:
PHP Fatal error: Uncaught Error: Undefined constant "SCRIPT_ROOT"
PHP Fatal error: Uncaught Error: Undefined constant "Y"
PHP Fatal error: Uncaught Error: Undefined constant "email"
These all point to the same type of issue: code that worked fine on PHP 7.4 but fails on PHP 8.
The Most Common Breaking Changes
Unquoted Constant Names in define()
This is probably the most common one. In PHP 7, you could write this, and it would work:
// ❌ Broken on PHP 8
define(SCRIPT_ROOT, "https://yoursite.com/");
PHP 8 throws a fatal error because SCRIPT_ROOT is not a quoted string. It tries to find a constant called SCRIPT_ROOT before it has been defined.
The fix is simple, just add quotes around the constant name:
// ✅ Works on both PHP 7 and PHP 8
define('SCRIPT_ROOT', "https://yoursite.com/");
Same rule applies everywhere you use define(). The first argument must always be a quoted string.
2. Unquoted Array Keys
In PHP 7, using bare words as array keys was allowed:
// ❌ Broken on PHP 8
$_SESSION[email]
$_COOKIE[username]
$_POST[phone]
PHP 8 treats email, username, and phone as constants, and since they are not defined, it throws a fatal error.
Always quote your array keys:
// ✅ Correct
$_SESSION['email']
$_COOKIE['username']
$_POST['phone']
3. Unquoted Strings in date () Calls
This one shows up as Undefined constant "Y" in your error log. It happens when the date format string is passed without quotes:
// ❌ Broken on PHP 8
echo date(Y);
echo date(Y-m-d);
Again, PHP 8 tries to look up Y as a constant and fails. The fix:
// ✅ Correct
echo date('Y');
echo date('Y-m-d');
Quick Reference Table
| Error in Log | What It Means | Fix |
|---|---|---|
Undefined constant "SCRIPT_ROOT" | Unquoted name in define() | Add quotes: define('SCRIPT_ROOT', ...) |
Undefined constant "Y" | Unquoted string in date() | Add quotes: date('Y') |
Undefined constant "email" | Unquoted array key | Add quotes: $_SESSION['email'] |
Two Ways to Handle This
Option 1: Roll Back to PHP 7.4 (Quick Fix)
If your site just broke and you need it live immediately, rolling back the PHP version is the fastest option. On cPanel with CloudLinux:
- Log in to cPanel
- Go to MultiPHP Manager
- Find your domain
- Select PHP 7.4 from the dropdown
- Click Apply
Your site will be back up within seconds. Keep in mind that PHP 7.4 is end-of-life and no longer receives security updates. This should be a temporary fix, not a permanent one.
Option 2: Fix the Code (Proper Fix)
The right long-term solution is to update the code to be PHP 8 compatible. The fixes are small. It is mostly a matter of adding missing quotes in the right places.
If you’re on shared hosting and have SSH or cPanel Terminal access, you can find all affected files quickly:
# Find unquoted date() calls
grep -rn "date(Y" /home/username/public_html/ --include="*.php"
# Find unquoted array keys with 'email'
grep -rn '\[email\]' /home/username/public_html/ --include="*.php"
Once you know which files are affected, you or your developer can fix them one by one.
Before Your Next Migration, A Short Checklist
- Take a full backup before any server or PHP version change
- Test on a staging environment first if possible
- Check your PHP error log immediately after migration to catch issues early
- Scan your codebase for PHP 8 incompatibilities before upgrading tools like PHPCompatibility can help
Summary
Things PHP 7 used to overlook silently, like unquoted constant names, unquoted array keys, and unquoted date format strings, now cause fatal errors. The fixes are straightforward, but they can be spread across many files, which is why sites often break after a migration without anyone expecting it.
If you’re a client on our hosting and your site broke after a server migration, contact our support team. We can check your error logs and get it sorted.
Published by Veeble Hosting · Support Team KB