Opcode Optimisation
Also Known As
opcache optimizer
constant folding
dead code elimination
PHP optimizer
TL;DR
PHP's compiler applies optimisation passes to the opcode array before caching — constant folding, dead code elimination, and pass-through minimisation reduce instruction count.
Explanation
PHP's Optimizer (part of OPcache) applies multiple optimisation passes to compiled opcodes: constant folding (2 + 3 compiled to 5), dead code elimination (if (false) { } removed), array/string merging (concatenated literals merged), pass-through optimisation (unnecessary RETURN opcodes removed), and live variable analysis (unused variables eliminated). PHP 7+ uses type inference during optimisation, enabling more aggressive dead code elimination with typed code. These optimisations happen once at compile time and are cached — they have zero runtime overhead once in OPcache.
Common Misconception
✗ Manual micro-optimisations like pre-computing constants improve PHP performance — OPcache's constant folding computes 60 * 60 * 24 at compile time; manual pre-computation adds clutter with zero performance benefit.
Why It Matters
Understanding OPcache optimisations explains why premature micro-optimisations are pointless in PHP (the compiler already does them) and where real performance gains come from (I/O, algorithms, caching).
Common Mistakes
- Pre-computing constants manually — PHP optimiser does this at compile time.
- Avoiding clear code for micro-optimisation — the optimiser handles constant expressions.
- Not enabling OPcache — without it, no optimisations apply on any request.
- Thinking opcache.optimization_level controls all passes — different passes have different levels.
Code Examples
✗ Vulnerable
// Unnecessary manual micro-optimisation:
$secsPerDay = 86400; // Pre-computed manually
$secsPerWeek = 604800; // Clutter — compiler does this
$bytesPerMB = 1048576;
// Complicated null check to avoid function call:
if (isset($arr['key']) && $arr['key'] !== null) { } // Premature
✓ Fixed
// Let the compiler optimise:
$secsPerDay = 60 * 60 * 24; // Compiler folds to 86400
$secsPerWeek = 60 * 60 * 24 * 7; // Compiler folds to 604800
$bytesPerMB = 1024 * 1024; // Compiler folds to 1048576
// Readable null check:
if (isset($arr['key'])) { } // Clear intent, same performance
// View opcache stats:
$status = opcache_get_status();
// Check: ['opcache_statistics']['hits'] vs ['misses']
// High miss rate = too many files or opcache.max_accelerated_files too low
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
25
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 7
Amazonbot 6
Google 4
Unknown AI 2
Ahrefs 1
Also referenced
How they use it
crawler 17
crawler_json 3
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Low
⚡ Quick Fix
Enable OPcache with opcache.optimization_level=0x7FFFBFFF (all optimisations) — the optimiser eliminates dead code, constant folds, and simplifies expressions before caching
📦 Applies To
PHP 5.5+
web
cli
🔗 Prerequisites
🔍 Detection Hints
OPcache enabled but optimization_level not set; dead code in PHP not removed at compile time; constant expressions computed at runtime
Auto-detectable:
✓ Yes
opcache-gui
vld
blackfire
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: Medium
✓ Auto-fixable
Fix: Low
Context: File