Migrating from PHP 5.x to PHP 7 and 8
Also Known As
PHP 5 to 7 migration
PHP upgrade
PHP 7 compatibility
TL;DR
PHP 7 removed mysql_*, ereg functions, and several deprecated features — a systematic approach using Rector and PHPCompatibility catches 95% of issues automatically.
Explanation
Migrating from PHP 5.x to 7/8 involves several categories. Removed: mysql_* extension, ereg_* functions, magic_quotes_gpc, safe_mode, call-time pass-by-reference, and mcrypt. Changed: strict comparison semantics (0 == 'string' is now false in PHP 7), error handling (many fatals become catchable Errors), constructor-style constructors. Tools: Rector auto-fixes most mechanical changes, phpcs with PHPCompatibility standard flags version-specific issues. Running tests on a PHP 7 Docker container before deploying is the safest approach.
Common Misconception
✗ Migrating from PHP 5.6 to 7 requires rewriting your application — most migrations are mechanical find-and-replace operations that Rector can automate.
Why It Matters
PHP 5.6 reached EOL in December 2018 — any production server still on PHP 5 has years of unpatched vulnerabilities. Migration is urgent.
Common Mistakes
- Not running the full test suite on PHP 7 before deploying
- Ignoring Rector in favour of manual migration
- Not checking third-party packages for PHP 7 compatibility
- Leaving mysql_* calls for later and being surprised by fatal errors in production
Code Examples
✗ Vulnerable
// PHP 5 patterns that break in PHP 7:
ereg('^[a-z]+$', $input); // Fatal — ereg removed
mysql_query($sql); // Fatal — mysql_ removed
list($a, $b) = $arr; // OK but some list() behaviour changed
✓ Fixed
// PHP 7 equivalents:
preg_match('/^[a-z]+$/', $input);
$pdo->prepare($sql)->execute();
[$a, $b] = $arr; // Short list syntax
// Run Rector before upgrading:
// vendor/bin/rector process --dry-run
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
22 Mar 2026
Edited
23 Mar 2026
Views
18
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 8
Perplexity 2
Google 2
Meta AI 1
Ahrefs 1
Also referenced
How they use it
crawler 12
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🔴 Critical
⚙ Fix effort: High
⚡ Quick Fix
Run composer require --dev rector/rector, create rector.php targeting PHP_80, then run vendor/bin/rector process src/ and fix remaining issues manually
📦 Applies To
PHP 5.0+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
mysql_connect() mysql_query(); ereg() eregi(); call-time pass-by-reference &$param in call sites
Auto-detectable:
✓ Yes
rector
phpcs
phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: High
False Positives: Low
✓ Auto-fixable
Fix: Medium
Context: File
Tests: Update